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

问题描述

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

  • 如何总结每个选择的所有价格?

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

我在 def 内、外尝试了 if 语句。就像如果输入答案将是 = 1,那么价格将是 1,20 等并试图总结它,这是行不通的。 非常感谢您的帮助!

prices = [1.20,1.30,2.00,1.60,2.50,4.00,4.80,3.80,3.25,3.40,2.70,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()

然后我应该总结客户想要订购的每件商品的所有价格。

解决方法

我建议你有一个主循环,然后有 2 个部分

  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

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...