-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexceptions.py
More file actions
56 lines (40 loc) · 1.14 KB
/
exceptions.py
File metadata and controls
56 lines (40 loc) · 1.14 KB
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
x = 2
try:
x = 1/0
except:
pass
y = "dog"
# user input
# this will only turn true and continue if a can be converted into an int
# this is turned into a function/method so it can be called multiple times
def give_num():
a_success = False
a = input("Give me a number: ")
while not a_success:
try:
a = int(a)
a_success = True
except:
print("Gave me a bad input, try again.")
a = input("Give me a number: ")
return a
x = give_num()
y = give_num()
# defining own type of error
class TwoDivisionError(Exception):
pass
# hasn't been defined yet, just that the thing exists. How? Python doesn't know
division_success = False
while not division_success:
try:
if y == 2:
raise TwoDivisionError("We aren't allowed to divide by 2 either.")
print(x/y)
division_success = True
except TwoDivisionError as e:
print("I cannot divide by 2! Give me a new y.")
y = give_num()
except ZeroDivisionError as e:
print(e)
print("I can't divide by 0! Give me a new y.")
y = give_num()