-
Notifications
You must be signed in to change notification settings - Fork 0
/
playground.py
64 lines (52 loc) · 1.46 KB
/
playground.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
#Example 1
try:
file = open("unexistent.txt")
a_dictionary = {"key": "value"}
print(a_dictionary["asd"])
except FileNotFoundError:
file = open("unexistent.txt", "w")
file.write("Something")
except KeyError as error_message:
print(f"That key {error_message} does not exist.")
else:
content = file.read()
print(content)
finally:
file.close()
print("File was closed.")
raise ZeroDivisionError("This a manually raised error")
# Example 2
# height = float(input("Height: "))
# weight = float(input("Weight: "))
# if height > 3:
# raise ValueError("Human Height should not be over 3 meters")
# bmi = weight / height ** 2
# print(bmi)
# Example 3
fruits = ["Apple", "Pear", "Orange"]
# Catch the exception and make sure the code runs without crashing.
# def make_pie(index):
# try:
# fruit = fruits[index]
# print(fruit + " pie")
# except IndexError:
# print("Fruit pie")
# make_pie(4)
# Example 4
# 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:
# pass
# return total_likes
# count_likes(facebook_posts)