【问题标题】:How can i print out output based on user input如何根据用户输入打印输出
【发布时间】: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


【解决方案1】:

坦率地说,我不明白你的问题是什么。循环工作并要求下一杯饮料,它会显示饮料。

但是,如果您想先获取所有饮料,然后再显示所有饮料,那么您应该为此创建单独的循环 - 首先仅用于获取饮料,其次仅用于显示饮料。

类似的东西。

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

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

【讨论】:

  • longest_name = max(names, key=len)longest_name_len = len(longest_name) 这部分是指名称然后给出间距的长度吗?
  • 是的,我用它来查找菜单中最长的名称 - 然后我可以在显示菜单时使用这个长度添加空格。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-05-19
  • 1970-01-01
  • 2012-07-20
  • 2021-01-19
  • 1970-01-01
  • 1970-01-01
  • 2013-10-03
相关资源
最近更新 更多