簡體   English   中英

為什么我的變量不會更新它們的值?

[英]Why won't my variables update their values?

我正在使用 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)

為什么這個 while True 循環不更新宏變量?

burger_toppings = input(...) - 所以它將等於輸入的內容,而不是 boolean True 您可以將 if 語句更改為if burger_toppings: ,如果 burger_toppings 為真(非空字符串、非空列表、非無 object 等),它將評估為 True。

您的代碼檢查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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM