-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathError_Testing.py
More file actions
36 lines (29 loc) · 872 Bytes
/
Error_Testing.py
File metadata and controls
36 lines (29 loc) · 872 Bytes
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
# #IndexError Assignment
fruits = ["Apple", "Pear", "Orange"]
# Catch the exception and make sure the code runs without crashing.
def make_pie(index):
try:
fruit = fruits[index]
except IndexError:
print("Fruit pie")
else:
print(fruit + " pie")
make_pie(4)
# #KeyErrorHandling Assignment
facebook_posts = [
{'Likes': 21, 'Comments': 2},
{'Likes': 13, 'Comments': 2, 'Shares': 1},
{'Likes': 33, 'Comments': 8, 'Shares': 3},
{'Comments': 4, 'Shares': 2},
{'Comments': 1, 'Shares': 1},
{'Likes': 19, 'Comments': 3}
]
def count_likes(posts):
total_likes = 0
for post in posts:
try:
total_likes = total_likes + post['Likes']
except KeyError as error_message:
print(f"KeyErro: {error_message} Handled !!")
return total_likes
count_likes(facebook_posts)