Search code examples
pythondictionaryinputwhile-loop

Continuous Loop with User Input for Shopping List - Adding the Total and Printing Grand Total


  1. I've created an empty dictionary, so when the user uses the program, the items they purchase will be added.
  2. And then finally, printed when they exit the program.
  3. I want to have the user be able to purchase in a continuous loop, selecting from products (not seen here) of products, until they decide to leave the program. Calculating the total cost * per weight each time - PROBLEM AREA!!!
  4. When user decides to exit (2) provide/calculate total weight * price (per kG) - DON'T KNOW HOW!!!
  5. Nor, can I print the Grocery List to screen - ISSUE HERE TOO!!!
# dictionary to store productID("ID"), price("price")
#with empty list as their values
groceryList ={"productID":[], "weight": [], "price":[]}
 
# converting dictionary to list for further updation
buyersList = list(groceryList.values())
 
# variable iD value of "productID" from dictionary 'buyersList'
iD = buyersList[0]

# variable weight value of "quantity" from dictionary 'buyersList'
kG = buyersList[1]

# variable qu value of "price" from dictionary 'a'
productPrice = buyersList[2]
 
total = 0
 
# This loop terminates when user select 2.EXIT option when asked
# in try it will ask user for an option as an integer (1 or 2) 
# if correct then proceed with an if/else statement asking for user input

# It should continue and keep adding the total price * weight - BUT THIS FUNCTION DOESN'T WORK

while True:
    try:
        choice = int(input("1.SELECT\n2.EXIT\nPlease select 1 or 2 (from the menu) here : ")) #use new line formatting
    if choice == 1:
        i = int(input("Please enter the product ID here : "))
        # input quantity of product
        w = float(input("Please enter how many KG you would like : "))
    
        p = float(input("Please enter the price of the product here : "))

        # append value of in "name", "quantity", "price"
        iD.append(i)  
        # as na = b[0] it will append all the value in the
        # list eg: "name":["rice"]
        productPrice.append(p)     
 
        # after appending new value the sum in price
        # as to be calculated
        total = lambda w, p: w * p
        print("\nYour current total amount is", total(w, p))
            
    elif choice == 2:
        break
        
    else:
        print("Sorry, that option isn't available. Please, try again.")
        
except ValueError:
    print("Invalid response. Please try again.")
    
#when user asks to exit loop - their grocery list is printed to screen
print("\n\n\nGROCERY LIST")

for i in range(len(buyersList)):
  print(iD[i], kG[i], productPrice[i])

I want to print the grand total of all their options


Solution

  • After the user enters the weight and price, you should add w * p to total, not redefine it as a lambda function. Then just print this variable.

    In the loop at the end, you should loop over the iD, kG, and productPrice lists, not the length of the dictionary groceryList. You can loop over them in parallel using zip().

    # dictionary to store productID("ID"), price("price")
    #with empty list as their values
    groceryList ={"productID":[], "weight": [], "price":[]}
    
    # converting dictionary to list for further updation
    buyersList = list(groceryList.values())
    
    # variable iD value of "productID" from dictionary 'buyersList'
    iD = buyersList[0]
    
    # variable weight value of "quantity" from dictionary 'buyersList'
    kG = buyersList[1]
    
    # variable qu value of "price" from dictionary 'a'
    productPrice = buyersList[2]
    
    total = 0
    
    # This loop terminates when user select 2.EXIT option when asked
    # in try it will ask user for an option as an integer (1 or 2) 
    # if correct then proceed with an if/else statement asking for user input
    
    # It should continue and keep adding the total price * weight - BUT THIS FUNCTION DOESN'T WORK
    
    while True:
        try:
            choice = int(input("1.SELECT\n2.EXIT\nPlease select 1 or 2 (from the menu) here : ")) #use new line formatting
            if choice == 1:
                i = int(input("Please enter the product ID here : "))
                # input quantity of product
                w = float(input("Please enter how many KG you would like : "))
    
                p = float(input("Please enter the price of the product here : "))
    
                # append value of in "name", "quantity", "price"
                iD.append(i)  
                # as na = b[0] it will append all the value in the
                # list eg: "name":["rice"]
                kG.append(w)
                productPrice.append(p)     
    
                # after appending new value the sum in price
                # as to be calculated
                total += w * p
                print("\nYour current total amount is", total)
    
            elif choice == 2:
                break
    
            else:
                print("Sorry, that option isn't available. Please, try again.")
    
        except ValueError:
            print("Invalid response. Please try again.")
            
    #when user asks to exit loop - their grocery list is printed to screen
    print("\n\n\nGROCERY LIST")
    
    for id, weight, price in zip(iD, kG, productPrice):
        print(id, weight, price)