简体   繁体   中英

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.

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. I'm assuming you want to use x to eventually exit the code. You can do this by adding another elif for the user to say their order is complete:

    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. 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. 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!

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.

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}")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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