-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathTry Except Value Error..py
More file actions
96 lines (74 loc) · 3.68 KB
/
Try Except Value Error..py
File metadata and controls
96 lines (74 loc) · 3.68 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
while True:
try:
number=int(input('\nGive Me "10": ').strip())
if number==10:
print(f'\nYou gave me "{number}", so I broke out of the conditonal \
while-loop example.')
break
elif number<10:
print(f'\nOops! You give me "{number}", which is too small. I won\'t \
stop this condional while-loop example, until you give me "10".')
elif number>10:
print(f'\nOops! You give me "{number}", which is too big. I won\'t stop \
this condional while-loop example, until you give me "10".')
except ValueError:
print('\nThe \'try:\' and \'except ValueErorr:\' handlers prevent any unwanted \
value errors from occurring, via user input data.')
print('\nIf the user presses any letter keys instead of pressing number keys, \
the \'try:\' and \'except:\'block executes/runs.')
print('This is the end of the entire conditional while-loop example:')
'''----------------------------------------------------------------'''
# This is a basic layout of the 'try and except', 'finally' program example. The program
# does work fine, but it does nothing. The program example below simply shows the
# basic layout of the 'try and except', 'finally' statements. The 'finally' statement is
# executed no matter the outcome the 'try and except' handler block does. Note: you
# can also leave out the 'finally' statement if you like, but it can come in handy if you
# want the final outcome to execute no matter what the 'try and except' handler block
# does. The 'pass' statements are just empty placeholders for the empty code blocks
# until they are needed, via the programmer.
try:
pass
except:
pass
else:
pass
finally:
pass
'''----------------------------------------------------------------'''
# Here is the very same 'try and except' program example below. Type and
# execute/run the program and see what happens.
try:
message=int(input('Pick a number. ').lower().strip())
except ValueError:
print('Numbers only please.')
else:
print('You picked a number.')
finally:
print("'finally' executed no matter what.")
# The 'finally' statement is executed no matter the outcome the 'try and except'
# handler block does
'''----------------------------------------------------------------'''
# These two 'input' statements in this program example asks the user their name and
# their age, using the 'try:' and 'except:' error handlers.
name=input('\nWhat is your name please? ').lower().strip()
try:
age=int(input(f'\nHow old are you {name}? ').lower().strip())
print(f'\n{name}. You are {age} years old.')
except ValueError:
print('\nThe \'try:\' and \'except ValueError:\' block executes/runs whenever a letter \
key is pressed instead of a number key.')
'''----------------------------------------------------------------'''
# Now, put this very same program code above into a conditional while-loop and see
# what happens when the user tries to type letters, instead of typing numbers for their
# age. When the 'try:' statement is executed, the 'break' statement causes the
# conditional while-loop to break out and the 'print' statement ('End of program') is
# then executed.
name=input('\nWhat is your name please? ').lower().strip()
while True:
try:
age=int(input(f'\nHow old are you {name}? ').lower().strip())
print(f'\n{name}. You are {age} years old.')
break
except ValueError:
print('\nThe \'try:\' and \'except ValueError:\' block executes/runs whenever a \
letter key is pressed instead of a number key.')