-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtry_except.py
More file actions
39 lines (32 loc) · 1.09 KB
/
try_except.py
File metadata and controls
39 lines (32 loc) · 1.09 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
# example 1
def get_age():
age = int(input())
if 18 <= age <= 75:
return age
else:
raise ValueError('Invalid age.') # using raise like this
# TODO: Complete fat_burning_heart_rate() function
def fat_burning_heart_rate(age):
heart_rate = 0.7 * (220 - age)
return heart_rate
if __name__ == "__main__":
# try exception clock
try:
age = get_age()
rate = fat_burning_heart_rate(age)
print(f'Fat burning heart rate for a {age} year-old: {rate} bpm')
except ValueError as expt: # using the raise
print(expt)
print('Could not calculate heart rate info.')
# example 2
try:
user_num = int(input())
div_num = int(input())
quotient = user_num // div_num
print(quotient)
except ZeroDivisionError as z: # no need to raise as auto-detected
print(f"Zero Division Exception: {z}")
except ValueError as v: # no need to raise as auto-detected
print(f"Input Exception: {v}")
# e.g. prints this
# Input Exception: invalid literal for int() with base 10: 'user_num'