我是一名新程序員,我在24小時內教自己使用Sams Teach Yourself Python,其中一個excersises告訴我要重寫一段代碼,以允許職員在一個約束條件下制定訂單,它告訴我改變它以包含價格,所以我做了,但是我想用用戶輸入的價格並把它放在一個列表中總結出來並打印總數。但是,它不起作用。我該怎麼辦?我的追加命令不起作用(Python)
下面是代碼:
breakfast_special = "Texas Omelet"
breakfast_notes = "Contains brisket, horseradish cheddar"
lunch_special = "Greek patty melt"
lunch_notes = "Like the regular onem but with tzatziki sauce"
dinner_special = "Buffalo Steak"
dinner_notes = "Top loin with hot sauce and blue cheese, NOT BUFFALO MEAT"
while True:
meal_time = raw_input("Which mealtime do you want? [breakfast, lunch, dinner, q to quit]")
if meal_time == "q":
break
price = raw_input("Price: $")
price.append(price)
total_price = sum.price
if meal_time == "breakfast":
print "Specials for {} :".format(meal_time)
print breakfast_special
print breakfast_notes
elif meal_time == "lunch":
print "Specials for {} :".format(meal_time)
print lunch_special
print lunch_notes
elif meal_time == "dinner":
print "Specials for {} :".format(meal_time)
print dinner_special
print dinner_notes
else:
print "Sorry, but {} isn't a valid choice".format(meal_time)
print "Goodbye!"
print "Price: ${}".format(total_price)
先修正您的縮進。 –
你的變量和列表都得到相同的名稱'價格'。將列表名稱更改爲「價格」。然後調用它上面的'sum'函數。像這樣:'sum(prices)' –
「把它列入一個列表」 - 什麼列表?你從來沒有做過。你不能只追加到一個數字,並期望它爲你創建一個「列表」。 – TigerhawkT3