-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexception.py
More file actions
39 lines (34 loc) · 760 Bytes
/
exception.py
File metadata and controls
39 lines (34 loc) · 760 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
37
38
39
# Exception
try:
x = int(input("What's x? "))
# print(f"x is {x}")
except ValueError:
print("x is not an integer")
else:
print(f"x is {x}")
# print(f"x is {x}") #NameError: name 'x' is not defined
# Exception handling: function, break
def main():
y = get_int()
print(f"y is {y}")
def get_int():
while True:
try:
y = int(input("What's y? "))
except ValueError:
print("y is not integer")
else:
break
return y
main()
# Exception handling: function, pass
def main():
z = get_int("What's z? ")
print(f"z is {z}")
def get_int(prompt):
while True:
try:
return int(input(prompt))
except ValueError:
pass
main()