-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathException_Handling.py
72 lines (56 loc) · 1.92 KB
/
Exception_Handling.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# Syntax :-
# try:
# Code Block
# these statements are those which can probably have error.
# except:
# This block is optional.
# If the try block encounters an exception, this block will handle it.
# else:
# If there is no exception, this code block will be excuted by the python interpreter
# finally
# python interpreter will always excute this code.
# Examples of Exception Handling :-
# 1 Zero Division Error :
# try:
# print ("try block.")
# x=int(input ("Enter a number : "))
# y=int(input ("Enter another number : "))
# z=x/y
# except ZeroDivisionError:
# print ("except ZeroDivisionError block.")
# print("Division by zero is not accepted.")
# else:
# print ("else block.")
# print("Division = ",z)
# finally:
# print("finally block.")
# x=0
# y=0
# print ("Out of try,except,else and finally blocks.")
# 2 Raise an exception.
# try:
# x=int(input ("Enter a number upto 100 : "))
# if x>100:
# raise ValueError(x)
# except ValueError:
# print(x, " is out of allowed range.")
# else:
# print(x, "is within allowed range.")
# ------------------------------------------------------
# Assignment
# ------------------------------------------------------
print("Assignment")
blog_posts = [{'Photos':3,'Likes':21,'Comments':2,},{'Likes':13,'Comments':2,'Shares':1},
{'Photos':5,'Likes':33,'Comments':8,'Shares':3},{'Comments':4,'Shares':2},
{'Photos':8,'Comments':1,'Shares':1},{'photos':3,'Likes':19,'Comments':3}]
total_likes = 0
for post in blog_posts:
try:
post.keys() == "Likes"
total_likes = total_likes + post['Likes']
except:
pass
if("Likes" not in post.keys()):
post['Likes']=0
print("Total likes : ",total_likes)
print("Updated dictionary : ",blog_posts)