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:
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)
...