Home > Back-end >  Python: Practicing A Grocery List - Question regarding
Python: Practicing A Grocery List - Question regarding

Time:04-15

I am looking to ask the user to pick from the items list. I figured I could ask the user a designated amount of questions ("Enter your Items: ") and add all of the questions up (like q1, q2, q3, etc.).

But I would rather allow the user to pick an indefinite amount of items until they hit Enter and break the loop. THEN add all of the prices from their entry, as long as it matches the fmPrices dict.

import math


dollar = "$"

items = ["Eggs","Milk","Chips","Butter","Grapes","Salsa","Beer"]
items.sort() 

choice = ("Choose from the following items:")
print (choice)
print (items)

fmPrices = {
    "Eggs" : 2.99,
    "Milk": 3.99,
    "Chips": 4.29,
    "Butter": 3.29,
    "Grapes": 3.49,
    "Salsa": 40.99,
    "Beer": 1.99

}

while True:
    q1 = input("Enter your item: ")

    if q1 == '':
        break
    else: 
        input("Enter your item: ")


print ("Your estimated cost is: ")
total = round(fmPrices[q1],2)

print ("{}""{}" .format(dollar, total))

CodePudding user response:

mylist = []
total = 0
while True:
    q1 = input("Enter your item: ")
    if q1 == '':
        break
    if q1 not in fmPrices:
        print("Item is not in the list")
    else:
        mylist.append(q1)
        total  = fmPrices[q1]

print ("Your estimated cost is: ")
print( "$", round(total,2) )

CodePudding user response:

For example

cost = 0

while True:
    q1 = input("Enter your item: ")
    if q1 == '':
        break
    price = fmPrices.get(q1)
    if price == None:
        print("Item not available")
    else:
        cost  = price 

total = round(cost,2)
print(f"Your estimated cost is: {dollar}{total}")
  • Related