'Can't get my exception handling code to work
I'm getting an EOF error. I think it has something to do with my input(). Do I need to put a loop in somewhere to continuously use input? ..
Write a program that calculates an adult's fat-burning heart rate, which is 70% of the difference between 220 and the person's age respectively. Complete fat_burning_heart_rate() to calculate the fat burning heart rate.
The adult's age must be between the ages of 18 and 75 inclusive. If the age entered is not in this range, raise a ValueError exception in get_age() with the message "Invalid age." Handle the exception in main and print the ValueError message along with "Could not calculate heart rate info."
Ex: If the input is:
35
the output is:
Fat burning heart rate for a 35 year-old: 129.5 bpm
If the input is:
17
the output is:
Invalid age.
Could not calculate heart rate info.
def get_age():
age = int(input())
if age < 18 or age > 75:
raise ValueError("Invalid age.")
# TODO: Raise exception for invalid ages
return age
# TODO: Complete fat_burning_heart_rate() function
def fat_burning_heart_rate():
age = get_age()
heart_rate = (220 - age) * .70
return heart_rate
user_input = ""
if __name__ == "__main__":
try:
age = get_age()
heart_rate = fat_burning_heart_rate()
print('Fat burning heart rate for a', age, 'year-old:',
heart_rate, 'bpm')
except ValueError as excpt:
print(excpt)
print("Could not calculate heart rate info.")
# TODO: Modify to call get_age() and fat_burning_heart_rate()
# and handle the exception
Program errors displayed here
Traceback (most recent call last):
File "main.py", line 18, in <module>
heart_rate = fat_burning_heart_rate()
File "main.py", line 10, in fat_burning_heart_rate
age = get_age()
File "main.py", line 2, in get_age
age = int(input())
EOFError: EOF when reading a line
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
