为了账号安全,请及时绑定邮箱和手机立即绑定

价格不相加

价格不相加

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 关注
  • 95 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信