Grocery Calculator, Supermarket Calculator in python

 

Pyhton

Create a General store calculator in python:

Source code:


#General store calculator

sum = 0
res = {}
ind = 0

print("Enter the item name and their price like that\nrise 2")

while True:
usr_val = input("Enter: ")

if usr_val != "q":
item_name, price = usr_val.split()
res.update({item_name: price})
sum += int(price)
else:
for key, value in res.items():
print(f"{ind}. {key} -- {value}")
ind = ind + 1
break

print(f"Total : {sum}")
print("--------Thank you for shopping-----------")

#store the sum
# for price in res.values():
# sum += price
# print(f"Total : {sum}")

Alternative code:


def calcu_price(items):
tprice = 0
for item, price in items.items():
tprice += price

return tprice

items = {}

while True:
item_name = input("Enter the item name: ")
price = input("Enter the price: ")

items.update({item_name: price})

if item_name == "q":
break

tprice = calcu_price(items)

print("The total price is", tprice)

Description:

  • Start by creating a dictionary to store the item names and prices.
  • Create a function to calculate the total price of all the items. This function should iterate over the dictionary and add up the prices of all the items.
  • Create a loop to get the item names and prices from the user. The loop should continue until the user enters "q" to quit.
  • In each iteration of the loop, add the item name and price to the dictionary.
  • After the loop, call the function to calculate the total price and print the total price to the user.

Post a Comment

0 Comments