在Cafe程序中接受多个订单,并进行循环

2024-10-01 00:27:05 发布

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

我目前正在努力完成这项python学校的作业,我不知道如何添加循环以继续订购,以及如何同时订购咖啡和茶。我的程序只允许你点一份,茶或咖啡。我的Exception ValueError还导致向中杯和大杯咖啡中添加香草糖浆的顺序不唯一,并允许在不应该添加香草糖浆的情况下将其添加到小杯咖啡中。我在这方面遇到了很多麻烦,如果您能给我提供帮助或建议,我将不胜感激

这是我的代码:

    price = 0
print("Tea is 1, Coffee is 2.")
print("Small is 1, Medium is 2, Large is 3.")

try:
  drinkchoice=float(input("What drink would you like?: "))
  if(drinkchoice == 1):
      TeaAmount=float(input("How many would you like?: "))
  else:
    CoffeeAmount=float(input("How many would you like?: "))  
except ValueError: 
    print("Error, follow the directions!")
    exit()

try:
  drinksize=float(input("What size would you like?: "))
  if(drinksize == 3 and drinkchoice == 1):
    print("1 for yes, 2 for no")
    Oprah=float(input("Would you like the Oprah sponsered option for 0.75 more?: "))
  if(drinksize == 2 or 3 and drinkchoice == 2):
    print("1 for yes, 2 for no")
    vanilla=float(input("Would you like a shot of vanilla syurp for 0.50 more?: "))
except ValueError:
   print("Error, follow the directions!")
   exit()

if(drinkchoice == 1):
  if(drinksize == 1):
    price=price+2*TeaAmount
  if(drinksize == 2):
    price=price+3.50*TeaAmount
  if(drinksize == 3):
    price=price+4.65*TeaAmount
    if(Oprah == 1):
       teaPrice=round(price,2)
       price=price+.75*TeaAmount
       OprahPrice=.75*TeaAmount
    else:
      price=round(price,2)
  coffeePrice = 0
  vanillaPrice = 0

if(drinkchoice ==2):
  if(drinksize == 1):
    price=price+2.25*CoffeeAmount
  if(drinksize == 2):
    price=price+3.75*CoffeeAmount
    if(vanilla == 1):
      coffeePrice = round(price,2)
      price=price+.50*CoffeeAmount
      vanillaPrice=.50*CoffeeAmount
    else:
      price=round(price,2)
      vanillaPrice=0
  if(drinksize == 3):
    price=price+4.85*CoffeeAmount
    if(vanilla == 1):
      coffeePrice = round(price,2)
      price=price+.50*CoffeeAmount
      vanillaPrice=.50*CoffeeAmount
    else:
      price=round(price,2)
      vanillaPrice=0
  teaPrice = 0
  OprahPrice = 0


price = round(price,2)

print("You paid $", teaPrice, "for tea")
print("You paid $", coffeePrice, "for coffee")
print("You paid $", OprahPrice, "for the extra Oprah version")
print("You paid $",  vanillaPrice, "for the vanilla syrup")
print("Your bill is $", price)

Here is the assignment


Tags: theyouforinputifisfloatprice
1条回答
网友
1楼 · 发布于 2024-10-01 00:27:05

将此作为一个答案,因为它太大,无法发表评论

根据您的数据结构,您将更适合使用dictionary。这将使您的代码更易于阅读,更好地组织数据,更好地支持将来添加新饮料和新饮料尺寸

字典存储一个key和一个value,以后可以被dict[key]dict.get(key)调用

存储该数据的示例可以是:

drinks = {'tea': {'small': 1, 'medium': 2, 'large': 3}}

现在,当用户指定他们想要什么时,您的生活和代码变得更易于管理和理解

drinks['tea']['small']
#1

但是,如果tea不存在,那么这种调用键、值对的方法将返回一个KeyError,因此最好使用get()

drinks.get('tea', {}).get('small')
#1

如果此处的任何一个键都不存在,此行将返回None,它提供了一个安全的方法来调用您的值

你也可以把你的drink_options存储在字典里。当您需要更改定价时,只需更改dict中密钥对的值

对于循环,while True:是一种持续循环直到用户指定停止的好方法。但是需要注意的是,如果您不打破这个循环,它将无限期地运行

您可以使用break退出while循环

下面你可以找到一些阅读材料的链接,这些链接可能会帮助你了解与本作业有关的更多信息。学习愉快

链接


Python Official Documentation - Dictionaries

Python Official Documentation - Break & continue statements

While Loops

Dictionaries in Python

相关问题 更多 >