-
Notifications
You must be signed in to change notification settings - Fork 0
/
handle_it.205.py
52 lines (44 loc) · 1.14 KB
/
handle_it.205.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
# Handle it
# Demonstrates handling exceptions
# pg 206
#try/except
try:
num = float(input("Enter a number: "))
except:
print('Something went wrong.')
# specify exception type
try:
num = float(input("\nEnter a number: "))
except ValueError:
print('That was not a number.')
# multiple exception types
print()
for value in (None, "Hi!"):
try:
print('Attempting to convert', value, '-->', end=' ')
print(float(value))
except (TypeError, ValueError):
print('Something went wrong.')
# multiple exception types
print()
for value in (None, "Hi!"):
try:
print('Attempting to convert', value, '-->', end=' ')
print(float(value))
except TypeError:
print('Can only convert a string or a number.')
except ValueError:
print('Can only convert a string of digits.')
# get an exceptions agument
try:
num = float(input("\nEnter a number: "))
except ValueError as e:
print('That was not a number.')
print(e)
# try/except/else
try:
num = float(input("\nEnter a number: "))
except ValueError:
print('That was not a number.')
else:
print('You entered the number', num)