试图从定义中的用户输入汇总总成本

2024-05-23 20:58:39 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试为用户创建一个菜单。用户必须订购至少3件商品,之后需要决定是继续订购还是结束订购。我为此创建了while循环,现在这是我的问题

  • 我如何总结每种选择的所有价格

我在定义中包括了用户答案,否则我的循环就不能像我希望的那样工作。。。我需要总结每个不同价格的选择,这些价格需要匹配我创建的列表中的位置

我尝试了def内部和外部的if语句。比如,若输入的答案是=1,那个么价格将是1,20等等,并试图把它加起来,就行不通了。 非常感谢您的帮助

prices = [1.20,1.30,2.00,1.60,2.00,2.50,4.00,4.80,3.80,3.25,3.40,2.70,2.50,3.15,4.40] 

food_menu = ["sandwich","pizza","lemon cake","chips","salad","panini","yoghurt","pasta","bagel,"cheese wrap","cherry muffin","egg","blt","marmite sarnine","watermelon pieces"]

def order1():
  input("Please choose which treat you wish to order by typing its position number [1-15] and confirm with ENTER : ")
  input("Next item to order: ") 


def order2():
  input("Another item number to order: ")



if decision == "y":
  print ("Awesome! Let's get started!")
  order1()

i=1
x=""
while i>0:
  if decision == "y":
    order2()
    x = input("Keep adding more food for bigger discount?\ny - for yes\ns - for stop:\nYour answer: ")
    if (x == "s"):
      break
  elif decision == "n":
    print ("Please come back tomorrow or try again. Today you need to get minimum 3 items today!\U0001F641")
    print ("Bye bye!")
    i=0
    exit()
  else:
    print ("Wrong answer, please try again from the start.. \U0001F641 ")
    i=0
    exit()

之后,我应该总结客户想要订购的每种商品的所有价格


Tags: to答案用户forinputiffooddef
1条回答
网友
1楼 · 发布于 2024-05-23 20:58:39

我建议你有一个主回路,然后有两个部件

  1. 处理用户对产品的选择
  2. 处理是否继续的选择

另外dict是存储菜单和价格的最佳结构,它提供了简单的访问和非常好的测试包含

food_menu = {'sandwich': 1.2, 'pizza': 1.3, 'lemon cake': 2.0, 'chips': 1.6, 'salad': 2.0, 'panini': 2.5,
             'yoghurt': 4.0, 'pasta': 4.8, 'bagel': 3.8, 'cheese wrap': 3.25, 'cherry muffin': 3.4, 'egg': 2.7,
             'blt': 2.5, 'marmite sarnine': 3.15, 'watermelon pieces': 4.4}

decision = ""
count = 0
total = 0
print("Menu with prices:")
print(*food_menu.items(), sep="\n")
while True:
    item = input("Please choose which treat you wish to order by typing its position number [1-15] "
                 "and confirm with ENTER : ")

    if item in food_menu:
        total += food_menu[item]
        count += 1
    else:
        print("Wrong input")

    decision = input("Keep adding more food for bigger discount? (y - for yes / s - for stop) : ")
    if decision == "s":
        if count < 3:
            print("Please come back tomorrow or try again. Today you need to get minimum 3 items today!")
        else:
            print("Total price is:", total)
        break

相关问题 更多 >