'How to correct calculation error on Python
The following equations estimate the calories burned when exercising (source):
Men: Calories = [(Age x 0.2017) — (Weight x 0.09036) + (Heart Rate x 0.6309) — 55.0969] x Time / 4.184
Women: Calories = [(Age x 0.074) — (Weight x 0.05741) + (Heart Rate x 0.4472) — 20.4022] x Time / 4.184
Write a program using inputs age (years), weight (pounds), heart rate (beats per minute), and time (minutes). Output calories burned for men and women.
Ex: If the input is:
49
155
148
60
Then the output is:
Men: 489.7772466539196 calories
Women: 580.939531548757 calories
My code -
age_years = float(input('Enter your age in years:\n'))
weight_lbs = float(input('Enter your weight in pounds:\n'))
heart_rate = float(input('Enter your heart rate in beats per minute:\n'))
time_min = float(input('Enter total time in minutes:\n'))
men_calories = [(age_years * 0.2017) - (weight_lbs * 0.09036) + (heart_rate * 0.6309) - 55.0969] * (time_min / 4.184)
women_calories = [(age_years * 0.074) - (weight_lbs * 0.05741) + (heart_rate * 0.4472) - 20.4022] * (time_min / 4.184)
print("Men:" , men_calories)
Error: Traceback (most recent call last):
File "main.py", line 8, in <module>
men_calories = [(age_years * 0.2017) - (weight_lbs * 0.09036) + (heart_rate * 0.6309) - 55.0969] * (time_min / 4.184)
TypeError: can't multiply sequence by non-int of type 'float'
What am I doing wrong here?
Solution 1:[1]
In Python, []
are used to define lists (sometimes called arrays in other languages), not as separators for expressions such as ()
, so, just use parentheses instead:
...
men_calories = ((age_years * 0.2017) - (weight_lbs * 0.09036) + (heart_rate * 0.6309) - 55.0969) * (time_min / 4.184)
women_calories = ((age_years * 0.074) - (weight_lbs * 0.05741) + (heart_rate * 0.4472) - 20.4022) * (time_min / 4.184)
...
Solution 2:[2]
If you use the following expression it will round your answer up to the nearest ten thousandth. print('Women: {:.2f} calories_women') print('Men: {:.2f} calories_men') Hope that helps the next student out because the struggle is definitely REAL ???????
Solution 3:[3]
age_years = int(input())
weight_lbs = int(input())
heart_rate = int(input())
time_min = int(input())
men_calories = ((age_years * 0.2017) + (weight_lbs * 0.09036) +
(heart_rate * 0.6309) - 55.0969) * time_min / 4.184
women_calories = ((age_years * 0.074) - (weight_lbs * 0.05741) +
(heart_rate * 0.4472) - 20.4022) * time_min / 4.184
print('Women: {:.2f} calories'.format(women_calories))
print('Men: {:.2f} calories'.format(men_calories))
Solution 4:[4]
I actually made this lab harder that what it was. Here is what you need: Calories = ((Age x 0.2757) + (Weight x 0.03295) + (Heart Rate x 1.0781) — 75.4991) x Time / 8.368
Age=float(input())
Weight=float(input())
Heart_Rate=float(input())
Time=float(input())
Calories = float((Age * 0.2757) + (Weight * 0.03295) + (Heart_Rate * 1.0781) - 75.4991) * Time / 8.368
print('Calories: {:.2f} calories'.format(Calories))
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | MrGeek |
Solution 2 | Chris Little |
Solution 3 | Becca |
Solution 4 | Nanthakumar J J |