Change language

# Python | Maintaining a shopping list

Examples :

User GO with following question Enter Your budget: 500 1.Add an item 2.Exit Enter your choice: 1 Enter product: corn flour Enter quantity: 1.5 kg Enter Price: 100 Amount left: 400 1.Add an item 2.Exit Enter your choice: 1 Enter product: wheat Enter quantity: 2 kg Enter Price: 100 Amount left: 300 1.Add an item 2.Exit Enter your choice: 1 Enter product: corn flour Enter quantity: 2 kg Enter Price: 250 Amount left: 150 1.Add an item 2.Exit Enter your choice: 1 Enter product: rice Enter quantity: 5 kg Enter Price: 300 Can’t Buy the product ### (because budget left is 150) 1.Add an item 2.Exit Enter your choice: 1 Enter product: xyz Enter quantity: 1 kg Enter Price: 50 Amount left: 100 1.Add an item 2. Exit Enter your choice: 2 Amount left can buy you wheat GROCERY LIST is: Product name Quantity Price corn flour 2 kg 250 wheat 2 kg 100 xyz 1 kg 50

Try to understand the above problem statement and explanation of the output and try it yourself before you go to the solution.

Code: Python code for checking shopping list

 # This cycle will continue as long until the budget becomes integer or floating while True :    try : bg = float ( input ( "Enter your budget:" )) # if the budget is integer or floating point, it will be saved   # temporarily in the variable & # 39; s & # 39;   s = bg  except ValueError:   print ( " PRINT NUMBER AS A AMOUNT " ) continue else : break   # dictionary for storing goods ("name"), quantity ("quantity"), # price (& quot; price & quot;) with an empty list as values ​​ a = { "name" : [], "quant " : [], " price " : []}   # convert the dictionary to a list for further updates b = list (a.values ​​())   # variable na value & quot; name & quot ; from the dictionary & # 39; a & # 39; na = b [ 0 ]   # variable qu," quant "value from dictionary & # 39; a & # 39; qu = b [ 1 ]   # variable pr value & quot; price & quot; from the dictionary & # 39; a & # 39; pr = b [ 2 ]   # This loop ends when the user selects the 2.EXIT option when prompted # when trying, it will ask the user for an integer parameter (1 or 2) # if correct then continue, otherwise keep asking options while True : try : ch = int ( input ( " 1.ADD 2.EXIT Enter your choice: " )) except ValueError: print ( "ERROR: Choose only digits from the given option " )   continue   else : # check budget is greater than zero and option is selected # user - 1, i.e. add element if ch = = 1 and s" 0 :    # product name pn = input ( " Enter product name: " )  # enter the quantity of the product   q = input ( " Enter quantity: " )   # entry price of goods   p = float ( input ( "Enter price of the product:" ))    if p" s: # checks if budget price is less   print ( "CAN, T BUT THE PRODUCT" )    continue    else : # checks if the product name is in the list   if pn in na:  # find the index of this product ind = na.index (pn)    # remove quantity from "quantitative" product index qu.re move (qu [ind])      # remove price from the price index product pr.remove (pr [ind])    # insert the new value given user earlier qu.insert (ind, q)    # insert the new value given user earlier pr.insert (ind, p)    # subtract the price from the budget and assign # this is to & # 39; s & # 39; sum (pr) because pr = [100, 200] if # budget is 500 then s = bg-sum (pr) = 200 # after updating for the same product from index 0 # pr = [200, 200], so s = 100 s = bg - sum (pr)    print ( "amount left" , s) else :   # add value to & quot; name & quot;, & quot; quantity & quot;, & quot; price & quot;   na.append (pn)     # as na = b [0] will add all values ​​to # list eg: & quot; name & quot ;: [& quot; pic & quot;] qu.append (q)     # same for quantity and price   pr.append (p)    # after adding a new value, the amount in the price # how it will be calculated   s = bg - sum (pr)       print ( "amount left" , s)    # if the budget is reset to zero, output "NO BUDGET" elif s " = 0 :     print ( "NO BUDGET" )  else : break     # will print the amount left in the variable & # 39; s & # 39; print ( "Amount left: Rs." , s)    # if the remaining amount is equal to any amount in the price list if s in pr:  # then print the name of the product you can buy   print ( "Amount left can buy you a" , na [pr.index (s)])     print ( "GROCERY LIST" )   # print the final shopping list for i in range ( len (na)):  print (na [i], qu [i], pr [i])

## Shop

Best laptop for Excel

\$

Best laptop for Solidworks

\$399+

Best laptop for Roblox

\$399+

Best laptop for development

\$499+

Best laptop for Cricut Maker

\$299+

Best laptop for hacking

\$890

Best laptop for Machine Learning

\$699+

Raspberry Pi robot kit

\$150

Latest questions

PythonStackOverflow

Common xlabel/ylabel for matplotlib subplots

PythonStackOverflow

Check if one list is a subset of another in Python

PythonStackOverflow

How to specify multiple return types using type-hints

PythonStackOverflow

Printing words vertically in Python

PythonStackOverflow

Python Extract words from a given string

PythonStackOverflow

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

PythonStackOverflow

Python os.path.join () method

PythonStackOverflow

Flake8: Ignore specific warning for entire file

## Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

How to specify multiple return types using type-hints

Printing words vertically in Python

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically