简体   繁体   中英

Why won't my variables update their values?

I am using python to make a five guys nutrition calculator, and entered all the nutritional info into a dictionary. I have individual variables such as carbs, calories, protein, etc. and update them by adding the values inside them with the values from dictionaries. The dictionary is long, so the first couple keys are

fiveguys_menu = {'burger': {'hamburger':[700, 39, 39, 43, 19.5, 2, 430, 8, 2], 'cheeseburger':[770, 39, 43, 49, 23.5, 2.2, 790, 8, 2],...}
first_food = input("What're you tryna eat?  Please state whether you want a burger or fries, fatty.\n").lower().replace(" ", "")
if 'burger' in first_food:
    while True:
        burger_type = input('Out of hamburger, cheeseburger, baconburger, and bacon cheeseburger, which one do you want?\n').lower().replace(" ", "")
        if 'ham' in burger_type:
            calories = fiveguys_menu['burger']['hamburger'][0]
            carbs = fiveguys_menu['burger']['hamburger'][1]
            protein = fiveguys_menu['burger']['hamburger'][2]
            total_fat = fiveguys_menu['burger']['hamburger'][3]
            sat_fat = fiveguys_menu['burger']['hamburger'][4]
            trans_fat = fiveguys_menu['burger']['hamburger'][5]
            sodium = fiveguys_menu['burger']['hamburger'][6]
            sugar = fiveguys_menu['burger']['hamburger'][7]
            fiber = fiveguys_menu['burger']['hamburger'][8]
            print_message("One hamburger coming up.")
            print(calories, carbs, protein, total_fat, sat_fat, trans_fat, sodium, sugar, fiber)

However, when trying to update the macro variables with the toppings list, the variables will not update.

fiveguys_toppings = {'a1sauce':[15, 3, 0, 0, 0,0, 280, 2, 0], 'barbeque':[60, 15, 0, 0, 0, 0, 400, 10, 0], ...}

while True:
                    burger_toppings = input("The toppings available are A1 Sauce, barbeque, green pepper, grilled mushrooms, hot sauce, jalapenos, ketchup, lettuce, mayo, mustard, onions, pickles, relish, and tomatoes\nWhat toppings do you want?  Please be specific to the spelling listed. \n").lower().replace(" ", "")

                    if burger_toppings == True:
                        calories += fiveguys_toppings[burger_toppings][0]
                        carbs += fiveguys_toppings[burger_toppings][1]
                        protein += fiveguys_toppings[burger_toppings][2]
                        total_fat += fiveguys_toppings[burger_toppings][3]
                        sat_fat += fiveguys_toppings[burger_toppings][4]
                        trans_fat += fiveguys_toppings[burger_toppings][5]
                        sodium += fiveguys_toppings[burger_toppings][6]
                        sugar += fiveguys_toppings[burger_toppings][7]
                        fiber += fiveguys_toppings[burger_toppings][8]
                    print(calories, carbs, protein, total_fat, sat_fat, trans_fat, sodium, sugar, fiber)

Why does this while True loop not update the macro variables?

burger_toppings = input(...) - so it will equal whatever was input, not the boolean True . You can change your if statement to be if burger_toppings: , which will evaluate to True if burger_toppings is true-ish (not empty string, not empty list, not None object, etc).

Your code checks for burger_toppings to be True , which it never will be since it's a str . Try:

fiveguys_toppings = {
    'a1sauce': [15, 3, 0, 0, 0, 0, 280, 2, 0],
    'barbeque': [60, 15, 0, 0, 0, 0, 400, 10, 0], ...}

while True:
    burger_toppings = input(
        "The toppings available are A1 Sauce, barbeque, green pepper, "
        "grilled mushrooms, hot sauce, jalapenos, ketchup, lettuce, mayo, "
        "mustard, onions, pickles, relish, and tomatoes.\n"
        "What toppings do you want?  "
        "Please be specific to the spelling listed.\n"
    ).lower().replace(" ", "")

    if burger_toppings not in fiveguys_toppings:
        print(f"{burger_toppings.capitalize()} is not one of the options!")
        continue

    calories += fiveguys_toppings[burger_toppings][0]
    carbs += fiveguys_toppings[burger_toppings][1]
    protein += fiveguys_toppings[burger_toppings][2]
    total_fat += fiveguys_toppings[burger_toppings][3]
    sat_fat += fiveguys_toppings[burger_toppings][4]
    trans_fat += fiveguys_toppings[burger_toppings][5]
    sodium += fiveguys_toppings[burger_toppings][6]
    sugar += fiveguys_toppings[burger_toppings][7]
    fiber += fiveguys_toppings[burger_toppings][8]
    print(calories, carbs, protein, total_fat, sat_fat, trans_fat, sodium, sugar, fiber)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM