Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Variable undefined error when it is clearly defined?

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

NameError is raised is not a bug, it is the correct behavior, because calories_burned_per_minute is indeed undefined.

Why? Because your loop didn’t run. '' isn’t a member of ["WALK", "RUN", "JOG"]. activity in ["WALK", "RUN", "JOG"] == False

Your while loop didn’t run, because while loop 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:

while False:
    print(True)

What happens? Exactly Nothing.

Change while activity in ["WALK", "RUN", "JOG"] to while activity not in ["WALK", "RUN", "JOG"]

Then your logic of getting the input is completely wrong.

You should do this:

while activity not in ["WALK", "RUN", "JOG"]:
    activity = input("Invalid input. Will you WALK, RUN, or JOG? ").upper()

Then this:

functions = {'WALK': burnedWalking, 'RUN': burnedRunning, 'JOG': burnedJogging}
calories_burned_per_minute  = functions[activity](weight)

Using match case:

match activity:
    case 'WALK':
        func = burnedWalking
    case 'RUN':
        func = burnedRunning
    case 'JOG':
        func = burnedJogging
calories_burned_per_minute  = func(weight)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading