def standardCalBurn(weight, height, age):
calories_needed = 655 + (4.3 * weight) + (4.7 * height) - (4.7 * age)
print(f"{calories_needed:.1f} calories are needed per day to sustain weight of {weight}")
def burnedRunning(weight):
calories_burned_per_minute = weight * 0.095
return calories_burned_per_minute
def burnedWalking(weight):
calories_burned_per_minute = weight * 0.054
return calories_burned_per_minute
def burnedJogging(weight):
calories_burned_per_minute = weight * 0.0775
return calories_burned_per_minute
weight = float(input("Please enter your weight: "))
height = float(input("Please enter your height (in inches): "))
age = float(input("Please enter your age: "))
standardCalBurn(weight, height, age)
calories_to_burn = float(input("How many calories do you wish to burn while exercising? "))
activity = ""
while activity in ["WALK", "RUN", "JOG"]:
if activity == "WALK":
calories_burned_per_minute = burnedWalking(weight)
if activity == "RUN":
calories_burned_per_minute = burnedRunning(weight)
if activity == "JOG":
calories_burned_per_minute = burnedJogging(weight)
if activity not in ["WALK", "JOG", "RUN"]:
activity = input("Invalid input. Will you WALK, RUN, or JOG? ").upper()
minutes_to_burn = calories_to_burn / calories_burned_per_minute
print(f"You will burn {calories_burned_per_minute:.2f} calories per minute")
print(f"You will need to {activity} for {minutes_to_burn:.2f} minutes")
Whenever I try to run my code, it is saying calories_burned_per_minute is undefined when I clearly previously defined. Any help is appreciated. Thank you.
Traceback (most recent call last): File "", line 37, in NameError: name 'calories_burned_per_minute' is not defined
NameErroris raised is not a bug, it is the correct behavior, becausecalories_burned_per_minuteis indeed undefined.Why? Because your loop didn't run.
''isn't a member of["WALK", "RUN", "JOG"]. Soactivity in ["WALK", "RUN", "JOG"]is false.Your
whileloop didn't run, becausewhileloop only runs as long as condition is satisfied and stops when the condition is no longer met. Here your condition is never met.Try this:
What happens? Exactly Nothing.
Change
while activity in ["WALK", "RUN", "JOG"]towhile activity not in ["WALK", "RUN", "JOG"]Then your logic of getting the input is completely wrong.
You should do this:
Then this:
Using
matchcase:And you have three functions with only one value that is different, this is bad, just use one function and pass the second value as argument. Your function names also doesn't follow the Python naming convention, and your variable names are also overly long, and you don't inline variables that are immediately returned, and you
printinside functions, and you useinputfunction rather than taking arguments, and you didn't do type hinting...I have refactored your code and removed all the cruft:
Use like this: