[英]How can i print out output based on user input
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
How can i print out the drinks and quantity based on what the user enter at orders?如何根据用户在订单中输入的内容打印出饮料和数量?
This code is workable full code link: https://paste.pythondiscord.com/ifokevahax.py此代码是可行的完整代码链接: https://paste.pythondiscord.com/ifokevahax.py
Frankly, I don't understand what is your problem.坦率地说,我不明白你的问题是什么。 Loop works and ask for next drink and it displays drink.
循环工作并要求下一杯饮料,它会显示饮料。
But if you want to first get all drinks and later display all drinks then you should create separated loops for this - first only to get drinks, second only to display drinks.但是,如果您想先获取所有饮料,然后再显示所有饮料,那么您应该为此创建单独的循环 - 首先仅用于获取饮料,其次仅用于显示饮料。
Something like this.像这样的东西。
BTW: I added few other changes.顺便说一句:我添加了一些其他更改。
menu = {
'coffee': {'name': 'Coffee', 'price': 4.00},
'tea': {'name': 'Tea', 'price': 3.00},
'coca-cola': {'name': 'Coca-Cola', 'price': 2.00},
}
names = [item['name'] for item in menu.values()]
longest_name = max(names, key=len)
longest_name_len = len(longest_name)
for key, value in menu.items():
name = value['name']
price = value['price']
print(f"{name:{longest_name_len}} : ${price:.2f}")
def take_order():
list_of_drinks = list(menu.keys())
# --- get all drinks ---
orders = int(input("How many types of drink will u be ordering? "))
user_required_items = []
total_cost = 0
while orders:
drink = input("Please enter a drink:")
drink = drink.lower()
if drink not in list_of_drinks:
print('Wrong name.')
print('Available:', ', '.join(names))
else:
orders -= 1
qnt = int(input("Quantity:"))
price = menu[drink]['price']
cost = price * qnt
user_required_items.append( [drink, qnt, price, cost] )
total_cost += cost
# --- display all drinks ---
print('Receipt')
print('==============================')
for drink, qnt, price, cost in user_required_items:
print(f'Drink: {drink}')
print(f'Quantity: {qnt} * {price} = {cost}')
print('==============================')
print(f'Total: {total_cost}')
return total_cost #, user_required_items
take_order()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.