亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

價格不相加

價格不相加

qq_遁去的一_1 2023-09-12 17:45:50
為 IT 課程創建一個披薩訂購程序,幾乎完成了,但我目前遇到了一個我似乎無法解決或不知道如何解決的問題。當用戶選擇完披薩后,應該將他們選擇的披薩的總成本加起來,但問題是他們沒有加起來,而是價格保持不變 Name:JackTelephone:47347842ORDER:['2. Hawaiian pizza', 8.5]['1. Pepperoni pizza', 8.5]['3. Garlic cheese pizza (with choice of sauce)', 8.5]Total Price: 8.50這是他們必須選擇的價目表['1. Pepperoni pizza', 8.5]['2. Hawaiian pizza', 8.5]['3. Garlic cheese pizza (with choice of sauce)', 8.5]['4. Cheese pizza (with choice of sauce)', 8.5]['5. Ham and cheese pizza', 8.5]['6. Beef & onion pizza', 8.5]['7. Vegetarian pizza', 8.5]['8. BBQ chicken & bacon aioli pizza', 13.5]['9. Boneless pizza (italian style anchovy with no bones)', 13.5]['10. Pizza margherita', 13.5]['11. Meat-lover’s pizza', 13.5]['12. Tandoori pizza', 13.5]我不知道問題是否出在這段代碼中,但看起來確實如此。我最初嘗試使用“cost.append”,但它只出現了這樣的錯誤+ 不支持的操作數類型:“int”和“str”def Choice_of_pizza():    for i in range(1,pizza_no                   +1): #Repeats a number of times (number user has inputted)      while True:        try: #Validating inputs          pizza_kind = int(input("Choice of pizza(s):"))          if pizza_kind < 1:            print("Refer to PIZZA MENU for number order")            continue          if pizza_kind > 12:            print("Refer to PIZZA MENU for number order")            continue          else:            pizza = pizza_kind - 1 #Makes the list start at 1            cost.append(MENU[pizza_kind-1][0][pizza])            customerOrder.append(MENU[pizza_kind-1][pizza])            因此,我將其替換為“cost=+customerOrder[i][1]”,但即便如此,它在某種程度上也適用于添加的披薩名稱,但不適用于客戶詳細信息的價格。預期的目標是,當用戶一一輸入選項時,它會取出名稱并將價格放入成本列表中,但似乎并沒有這樣做。
查看完整描述

1 回答

?
嚕嚕噠

TA貢獻1784條經驗 獲得超7個贊

代碼問題:

  1. 對于加法賦值,語法為:

    x += y # 將 x 分配給 (x + y)

不是:

x?=?+y??#?this?assign?x?to?y
  1. Glbol 不會在您的代碼中造成問題,但通常會令人不悅并對編程技能產生負面影響,即為什么全局變量是邪惡的?。

Choice_of_pizza功能修復

def Choice_of_pizza():

? ? for i in range(1,pizza_no +1): #Repeats a number of times (number user has inputted)

? ? ?

? ? ? while True:

? ? ? ? try: #Validating inputs

? ? ? ? ? pizza_kind = int(input("Choice of pizza(s):"))

? ? ? ? ? if pizza_kind < 1:

? ? ? ? ? ? print("Refer to PIZZA MENU for number order")

? ? ? ? ? ? continue

? ? ? ? ? if pizza_kind > 12:

? ? ? ? ? ? print("Refer to PIZZA MENU for number order")

? ? ? ? ? ? continue

? ? ? ? ? else:

? ? ? ? ? ? pizza = pizza_kind - 1 #Makes the list start at 1

? ? ? ? ? ? print('\nYou have chosen {}\n'.format(MENU[pizza_kind-1][0]))

? ? ? ? ? ? customerOrder.append(MENU[pizza_kind-1])

? ? ? ? ? ? cost = 0

? ? ? ? ? ? for i in range(len(customerOrder)):

? ? ? ? ? ? ? cost += customerOrder[i][1]

? ? ? ? ? ? ? ? ? ? ?

? ? ? ? ? ? global total_cost? # globals are discouraged

? ? ? ? ? ? total_cost=0

? ? ? ? ? ? #Sum of the pizzas

? ? ? ? ? ? global Combined_Total # globals are discouraged

? ? ? ? ? ? if delivery == "D": #Adds $3 dollars to the total cost if delivery

? ? ? ? ? ? ? total_cost += cost

? ? ? ? ? ? ? Combined_Total = total_cost + Delivery_cost

? ? ? ? ? ? else: #Price stays the same if pick up

? ? ? ? ? ? ? total_cost += cost

? ? ? ? ? ? ? Combined_Total = total_cost

? ? ? ? ? ? break

? ? ? ? except ValueError:#Validating inputs - accepts only numbers and can't be left blank?

? ? ? ? ? print("Please use numbers only")

? ? ? ? ? continue


查看完整回答
反對 回復 2023-09-12
  • 1 回答
  • 0 關注
  • 117 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號