简体   繁体   English

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

[英]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.

相关问题 如何根据输入打印出正确的响应? - How can I print out the correct response based on input? 根据用户在破折号(或闪亮)中输入的打印输出 - Print output based on user input in dash (or shiny) 如何挑选出列表中的某些元素并通过用户输入获得 output? - How can I single out certain elements in a list and get an output with user input? 在 Python 中:如何让我的代码根据我的输入打印出我可以拼写的所有可能的单词? - In Python: How can i get my code to print out all the possible words I can spell based on my input? 如何根据用户输入显示if语句? - How do I get if statements to print based on user input? 如何使输出打印出数字列表? - How can I make the output print out a list of numbers? 如何从用户输入中捕获矩阵并在用户输入时将其打印出来? - How do I capture a matrix from a user input and print it out as the user input it? 通过用户输入打印出元组中每次不同的输出 - Print out a output that is different each time in a tuple via a user input 如何从 python 中的列表中获取特定的多个值,如果用户输入等于多个值,则打印 output - How to get the specific multiple value out from a list in python and If the user input is equal to the mulitple values print output 在 Python 中使用类根据用户输入打印出实例属性 - Using classes to print out instance attributes based on user input in Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM