为什么我的变量不更新其值?

时间:2020-06-07 20:26:43

标签: python

我正在使用python制作一个五个人的营养计算器,并将所有营养信息输入到字典中。我有一些变量,例如碳水化合物,卡路里,蛋白质等,并通过将它们中的值与字典中的值相加来更新它们。字典很长,所以前几个键是

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)

但是,当尝试使用顶部列表更新宏变量时,变量将不会更新。

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)

为什么在True循环中这样做不会更新宏变量?

2 个答案:

答案 0 :(得分:0)

burger_toppings = input(...)-因此它将等于输入的内容,而不是布尔值True。您可以将if语句更改为if burger_toppings:,如果burger_toppings是true-ish(非空字符串,非空列表,非None对象等),则其计算结果为True。

答案 1 :(得分:0)

您的代码检查burger_toppings是否为True,因为它是str,所以永远不会。试试:

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)