【发布时间】:2020-11-05 07:40:43
【问题描述】:
def take_order():
list_of_drinks = ["coffee", "tea", "coca-cola"]
user_required_items = []
orders = int(input("how many types of drink will u be ordering? "))
total_cost = 0
for i in range(orders):
drink = input("Please enter a drink:")
drink = drink.capitalize()
user_required_items.append(drink)
qnt = int(input("Quantity:"))
user_required_items.append(qnt)
price1 = 0
price2 = 0
price3 = 0
drink = drink.lower()
if drink == "coffee":
price1 = item["Coffee"]*qnt
elif drink == "tea":
price2 = item["Tea"]*qnt
else:
price3 = item["Coca-cola"]*qnt
total_cost += price1+price2+price3
print()
print('Receipt')
print('==============================')
print(f'{"Drink":5s}: {drink:>1s}')
print(f'{"Quantity":8s}:{qnt:>13}')
return total_cost
如何根据用户在订单中输入的内容打印出饮料和数量?
此代码可行 完整代码链接:https://paste.pythondiscord.com/ifokevahax.py
【问题讨论】:
-
代码是否有错误或问题,如果有,请发布回溯。
-
paste.pythondiscord.com/icehejuxiy.rb> 所以即使我输入了不在菜单中的饮料,代码仍将使用第一项的价格,我该如何防止这种情况发生。证据在链接中
-
以及如何让循环在打印“请从菜单中输入饮料”后返回询问用户他们想要的饮料
-
始终将所有信息作为有问题的文本,而不是链接到外部页面。
-
我不明白你为什么使用
'f'{"Quantity":8s}:{qnt:>13}',如果你可以直接输入一些文字f'Quantity:{qnt:>13}',它会更具可读性。
标签: python python-3.x for-loop if-statement