如何根据用户输入打印输出

时间:2020-07-15 18:01:34

标签: python python-3.x for-loop if-statement noobaa

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

1 个答案:

答案 0 :(得分:0)

坦率地说,我不明白您的问题是什么。 Loop可以正常工作并要求下一杯饮料,并显示饮料。

但是,如果要先获取所有饮料,然后再显示所有饮料,则应为此创建单独的循环-首先仅获取饮料,其次仅显示饮料。

类似这样的东西。

顺便说一句:我添加了一些其他更改。

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()