Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to keep adding the value of an item?

menu = {
    "Baja Taco": 4.00,
    "Burrito": 7.50,
    "Bowl": 8.50,
    "Nachos": 11.00,
    "Quesadilla": 8.50,
    "Super Burrito": 8.50,
    "Super Quesadilla": 9.50,
    "Taco": 3.00,
    "Tortilla Salad": 8.00
}

while True:
    # keep adding to the price if user prompts another item
    # i know the operation won't work its just what i want to happen
    try:
        x = input("Item: ")
        y = 0
        if x in menu:
            z = x + y
            print(f"Total: ${z}")
    # If u want to ctrl+d out and get ur price
    except (EOFError):
        print(f"Total: ${menu[x]}")
        break
    # so code can handle wrong inputs
    except (KeyError):
        pass

can’t find a way to make items add up

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Things you need to do:

  1. Define a variable for storing total outside the loop otherwise the variable will be overridden everytime
  2. Get the price from the menu dictionary and add it to the total

If both changes are done, the code should look something like this

total = 0
while True:
    try:
        x = input("Item: ")
        if x in menu:
             total = total + menu[x]
             print(f"Total: ${total}")
    # If u want to ctrl+d out and get ur price
    except (EOFError):
        print(f"Total: ${menu[x]}")
        break
    # so code can handle wrong inputs
    except (KeyError):
        pass
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading