简体   繁体   English

我如何添加多个输入

[英]How do i add multiple inputs

I made like a shopping system thing where you choose the item you want to buy and it then give you the total, I don't know how I can add the total of everything you chose.我做了一个购物系统,你选择你想买的东西,然后它给你总数,我不知道我怎么能把你选择的所有东西加起来。 This is the code currently:这是目前的代码:

x = 1

print("what would you like to buy")
print("milk = 10, juice = 5, icecream = 9")
print("type the item you want to buy")
x = 1
while x == 1:
         items_chosen= input("what would you like to buy ")

         if items_chosen in ["milk", "Milk"]:#if the input is milk write you bought milk
                  print("you bought milk ")
         elif items_chosen in ("juice", "Juice"):
                  print("you bought juice")
                  
         elif items_chosen in ("icecream",):
                  print("you bought icecream")

         else:
                  print("wrong input")

Something like this?是这样的吗? Simply add the prices.只需添加价格。

prices = {'milk' : 10, 'juice': 5, 'icecream' : 9}
total = 0

print("what would you like to buy")
print("milk = 10, juice = 5, icecream = 9")
print("type the item you want to buy")
x = 1
while x == 1:
         items_chosen= input("what would you like to buy ")

         if items_chosen in ["milk", "Milk"]:#if the input is milk write you bought milk
                  print("you bought milk ")
                  total += prices['milk']
         elif items_chosen in ("juice", "Juice"):
                  print("you bought juice")
                  total += prices['juice']
         elif items_chosen in ("icecream",):
                  print("you bought icecream")
                  total += prices['icecream']
         else:
                  print("wrong input")
         print("Current total: " + total) 

Simplest Addition最简单的加法

I'm assuming you're pretty new to python so here's the most simple addition I can think of to implement what you want.我假设你是 python 的新手,所以这是我能想到的最简单的添加来实现你想要的。

print("what would you like to buy")
print("milk = 10, juice = 5, icecream = 9")
print("type the item you want to buy")
x = 1
total_price = 0 # variable to keep track of the total price of your order
while x == 1:
    items_chosen= input("what would you like to buy ")

    if items_chosen in ["milk", "Milk"]:#if the input is milk write you bought milk
        print("you bought milk ")
        total_price += 10
    elif items_chosen in ("juice", "Juice"):
        print("you bought juice")
        total_price += 5
    elif items_chosen in ("icecream",):
        print("you bought icecream")
        total_price += 9
    else:
         print("wrong input")

    print("current total price of your order is:", total_price)

Better Practices and Fixes更好的做法和修复

Exiting退出

In your codes current state you have an infinite loop.在您当前的代码 state 中,您有一个无限循环。 I'm assuming you want to use x to eventually exit the code.我假设您想使用x最终退出代码。 You can do this by adding another elif for the user to say their order is complete:您可以通过添加另一个 elif 来让用户说他们的订单已完成:

    if items_chosen in ["milk", "Milk"]:#if the input is milk write you bought milk
        print("you bought milk ")
        total_price += 10
    elif items_chosen in ("juice", "Juice"):
        print("you bought juice")
        total_price += 5
    elif items_chosen in ("icecream",):
        print("you bought icecream")
        total_price += 9
    elif items_chosen in ("done"):
        x = 0
    else:
         print("wrong input")

Better Checking更好的检查

There's some hints in your code that you want to keep track of your order in a different way.您的代码中有一些提示您希望以不同的方式跟踪您的订单。 I won't propose a whole new rewrite without better understanding your intentions but I can propose at the simplest you modify the current way you're checking the user input.如果没有更好地理解您的意图,我不会提出全新的重写,但我可以建议您最简单地修改当前检查用户输入的方式。 You seem to have mixed up how to check for the strings but it works out in the end thankfully.您似乎混淆了如何检查字符串,但谢天谢地,它最终成功了。 Regardless a simpler more consistent way would be like so:不管更简单更一致的方式是这样的:

while x == 1:
    items_chosen = input("what would you like to buy ")
    items_chosen = items_chosen.lower() # make the user input all lower case so we don't need to give multiple capitalization forms

    if "milk" in items_chosen:
        print("you bought milk ")
        total_price += 10
    elif "juice" in items_chosen:
        print("you bought juice")
        total_price += 5
    elif "icecream" in items_chosen:
        print("you bought icecream")
        total_price += 9
    elif "done" in items_chosen:
        x = 0
    else:
         print("wrong input")

In this version you'd be checking if the item (ie "juice") is in the users input.在此版本中,您将检查该项目(即“juice”)是否在用户输入中。 In the version you had you were checking if the user input is in the the name of the item.在您拥有的版本中,您正在检查用户输入是否在项目名称中。 This alternative form doesn't require exact can account for simple typos like:这种替代形式不需要精确可以解释简单的拼写错误,例如:

type the item you want to buy
what would you like to buy juicee
you bought juice

Set a total variable, and add the price to it each time a valid item is chosen.设置一个total变量,并在每次选择有效项目时将价格添加到它。 To simplify adding the right price on each input, put the items and prices in a dictionary so you can look up the price by the item name;为了简化在每个输入上添加正确的价格,将项目和价格放入字典中,以便您可以通过项目名称查找价格; this also means you don't need to have an if for each item!这也意味着您不需要为每个项目设置if

Note that you haven't set a condition for when to stop asking for input, so it'll keep going until you Ctrl-C it.请注意,您没有设置何时停止要求输入的条件,因此它会一直持续到您按下 Ctrl-C 为止。

inv = {
    "milk": 10,
    "juice": 5,
    "icecream": 9
}
print("what would you like to buy")
print(", ".join(f"{item} = {price}" for item, price in inv.items()))
print("type the item you want to buy")
total = 0
while True:
    item = input("what would you like to buy ").lower()
    # maybe break the loop if item == "quit"?
    try:
        total += inv[item]
        print(f"you bought {item}")
        print(f"total so far: {total}")
    except KeyError:
        print("wrong input")
what would you like to buy
milk = 10, juice = 5, icecream = 9
type the item you want to buy
what would you like to buy Milk
you bought milk
total so far: 10
what would you like to buy IceCream
you bought icecream
total so far: 19
what would you like to buy Soup
wrong input
what would you like to buy Milk
you bought milk
total so far: 29
what would you like to buy

Here's what I would do.这就是我会做的。 This way the user can stop shopping and isn't stuck in an infinite loop!这样用户就可以停止购物并且不会陷入无限循环!

    shopping = True
    prices = {"milk": 10, "juice": 5, "icecream": 9}
    total = 0
    
    while shopping:
        item_chosen = input("What would you like to buy?\t")
        item_lowercase = item_chosen.lower()

        if item_lowercase in prices:
            total += prices[item_lowercase]
        else:
            print("I did not recognise that item!")
        carry_on_shopping = None
        while carry_on_shopping is None:
            carry_on = input("Carry on shopping? y/n \t")
            if carry_on == "y":
                carry_on_shopping = True
            elif carry_on == "n":
                carry_on_shopping = False
            else:
                print("Enter 'y' or 'n'")

    shopping = carry_on_shopping
    print(f"Total is {total}")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM