Question

10.11 LAB: Fat-burning heart rate Write a program that calculates an adult's fat-burning heart rate, which...

10.11 LAB: Fat-burning heart rate

Write a program that calculates an adult's fat-burning heart rate, which is 70% of 220 minus the person's age. 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.

Heres some of the code I have already. Please keep it similiar to this

def get_age():
try:
age = int(input())
if age < 18 or age > 75:
raise ValueError('Invalid age.')
return age

def fat_burning_heart_rate(age):
heart_rate = 154 - age
return heart_rate

if __name__ == "__main__":
except ValueError as excpt:
print(excpt)
print('Could not calculate heart rate info.')
age = get_age()

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Solution:

Program:

def get_age(): #function to read age from inputs
age=int(input())
if age<18 or age >75: #check age validity
raise ValueError("Invalid age.") #If age is not valid, raise exception
return age #If age is valid, return age
  

def fat_burning_heart_rate(age): #function to calculate heart rate
  
heart_rate=220-age #calculate 70% of 220 minus age
heart_rate*=0.7
return heart_rate #return heart rate

if __name__=="__main__":
try: #try block of statements
age=get_age()
rate=fat_burning_heart_rate(age) #call method to calculate heart rate
print("Fat burning rate for a ",age," year-old:",end="") #print result
print(rate,"bpm")
except ValueError as excpt: #If exception occurs, handle them with printing message on console
print(excpt)
print("Could not calculate heart rate info.")
  

Screenshots:

The screenshots of the program execution are attached below for reference. Please follow them for indentation.


Add a comment
Know the answer?
Add Answer to:
10.11 LAB: Fat-burning heart rate Write a program that calculates an adult's fat-burning heart rate, which...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Use program control statements in the following exercises: Question 1 . Write pseudocode for the following:...

    Use program control statements in the following exercises: Question 1 . Write pseudocode for the following: • Input a time in seconds. • Convert this time to hours, minutes, and seconds and print the result as shown in the following example: 2 300 seconds converts to 0 hours, 38 minutes, 20 seconds. Question 2. The voting for a company chairperson is recorded by entering the numbers 1 to 5 at the keyboard, depending on which of the five candidates secured...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT