简体   繁体   English

为什么我的变量不会更新它们的值?

[英]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.我正在使用 python 制作一个五人营养计算器,并将所有营养信息输入字典。 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?为什么这个 while True 循环不更新宏变量?

burger_toppings = input(...) - so it will equal whatever was input, not the boolean True . burger_toppings = input(...) - 所以它将等于输入的内容,而不是 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).您可以将 if 语句更改为if burger_toppings: ,如果 burger_toppings 为真(非空字符串、非空列表、非无 object 等),它将评估为 True。

Your code checks for burger_toppings to be True , which it never will be since it's a str .您的代码检查burger_toppings是否为True ,因为它是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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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