Python icon indicating copy to clipboard operation
Python copied to clipboard

Python Mini Project: Grocery Store Cart System I built a simple console-based Grocery Store application using Python 🐍 to strengthen my understanding of core programming concepts. 🔧 Features: Add items to cart Remove items from cart Calculate total price dynamically Menu-driven program using loops 📚 Concepts Used: Dictionaries Loops (while) Conditional statements User input handling This project helped me practice logic building and real-world problem solving using Python. 💡 More improvements coming soon! #Python #Programming #MiniProject #LearningByDoing #AIML #EngineeringStudent

Open ayyanagoud12 opened this issue 3 weeks ago • 1 comments

cart = {} prices = {"rice": 50, "milk": 30, "bread": 25, "sugar": 40}

while True: print("\n--- Grocery Store Menu ---") print("1. Add item") print("2. Remove item") print("3. View total price") print("4. Exit")

choice = int(input("Enter your choice: "))

if choice == 1:
    item = input("Enter item name: ").lower()
    if item in prices:
        qty = int(input("Enter quantity: "))
        cart[item] = cart.get(item, 0) + qty
        print(item, "added to cart")
    else:
        print("Item not available")

elif choice == 2:
    item = input("Enter item to remove: ").lower()
    if item in cart:
        del cart[item]
        print(item, "removed from cart")
    else:
        print("Item not in cart")

elif choice == 3:
    total = 0
    print("\nItems in cart:")
    for item, qty in cart.items():
        cost = prices[item] * qty
        total += cost
        print(item, "x", qty, "=", cost)
    print("Total Price =", total)

elif choice == 4:
    print("Thank you for shopping!")
    break

else:
    print("Invalid choice")

ayyanagoud12 avatar Dec 28 '25 18:12 ayyanagoud12

I think you should add try/catch blocks to avoid crashes:

Safe input for choice

try: choice = int(input("Enter your choice: ")) except ValueError: print("Please enter a valid number (1-4).") choice = 0 # Invalid choice will go to else

Option 1: Add item

if choice == 1: item = input("Enter item name: ").lower() if item in prices: try: qty = int(input("Enter quantity: ")) if qty <= 0: print("Quantity must be positive.") else: cart[item] = cart.get(item, 0) + qty print(f"{item} added to cart.") except ValueError: print("Quantity must be a number.") else: print("Item not available.")

Option 2: Remove item

elif choice == 2: item = input("Enter item to remove: ").lower() if item in cart: del cart[item] print(f"{item} removed from cart.") else: print("Item not in cart.")

Option 3: Show total

elif choice == 3: if not cart: print("Your cart is empty.") else: total = 0 print("\nItems in cart:") for item, qty in cart.items(): cost = prices[item] * qty total += cost print(f"{item} x {qty} = {cost}") print(f"Total Price = {total}")

Option 4: Exit

elif choice == 4: print("Thank you for shopping!") break

Invalid choice

else: print("Invalid choice. Please select 1–4.")

Mike323334 avatar Dec 28 '25 19:12 Mike323334

Nice mini project! 👏
One possible enhancement could be an Update Item Quantity feature instead of forcing users to remove and re-add items.

For example:

  • Add a new menu option: Update item
  • Allow the user to change quantity (increase/decrease)
  • If updated quantity becomes 0, remove the item automatically

This would make the cart flow more user-friendly.

vanshika114 avatar Jan 02 '26 06:01 vanshika114

this project could become much more fun if we add something like discount coupons inventory management prevent adding item beyond its availability

shivansh31414 avatar Jan 08 '26 06:01 shivansh31414