Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
25 views

CS coding

The document outlines a shopping cart system implemented in Python, allowing users to view available items, add or remove items from their cart, view the cart, and proceed to checkout. It includes a predefined list of store items with prices and provides a menu-driven interface for user interaction. The program continues to run until the user chooses to exit.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

CS coding

The document outlines a shopping cart system implemented in Python, allowing users to view available items, add or remove items from their cart, view the cart, and proceed to checkout. It includes a predefined list of store items with prices and provides a menu-driven interface for user interaction. The program continues to run until the user chooses to exit.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Source Code:-

# Shopping Cart System

# Predefined store items (item: price)

store_items = {

"Apple": 0.5,

"Banana": 0.3,

"Orange": 0.7,

"Milk": 1.5,

"Bread": 2.0,

"Eggs": 3.0

# Shopping cart (item: quantity)

shopping_cart = {}

while True:

# Display menu

print("\n=== Shopping Cart Menu ===")

print("1. View Available Items")

print("2. Add Item to Cart")

print("3. Remove Item from Cart")

print("4. View Cart")

print("5. Checkout")

print("6. Exit")

choice = input("Choose an option (1-6): ")

if choice == "1":

7
# View available items

print("\nAvailable Items:")

for item, price in store_items.items():

print(f"- {item}: ${price:.2f}")

elif choice == "2":

# Add item to cart

item = input("Enter the name of the item to add: ").title()

if item in store_items:

quantity = int(input("How many{item}(s) would you like to add?”))

if item in shopping_cart:

shopping_cart[item] += quantity

else:

shopping_cart[item] = quantity

print(f"{quantity} {item}(s) added to the cart.")

else:

print("Item not found in the store.")

elif choice == "3":

# Remove item from cart

item = input("Enter the name of the item to remove: ").title()

if item in shopping_cart:

quantity = int(input("How many {item}(s) would you like to


remove? "))

if quantity >= shopping_cart[item]:

shopping_cart.pop(item)

print(f"Removed all {item}(s) from the cart.")

8
else:

shopping_cart[item] -= quantity

print(f"Removed {quantity} {item}(s) from the cart.")

else:

print("Item not found in the cart.")

elif choice == "4":

# View cart

if not shopping_cart:

print("\nYour cart is empty.")

else:

print("\nItems in your cart:")

total = 0

for item, quantity in shopping_cart.items():

price = store_items[item] * quantity

print(f"- {item}: {quantity} @ ${store_items[item]:.2f} each


= ${price:.2f}")

total += price

print(f"\nTotal: ${total:.2f}")

elif choice == "5":

# Checkout

if not shopping_cart:

print("\nYour cart is empty. Add items before checking out.")

else:

print("\nItems in your cart:")

9
total = 0

for item, quantity in shopping_cart.items():

price = store_items[item] * quantity

print(f"- {item}: {quantity} @ ${store_items[item]:.2f} each


= ${price:.2f}")

total += price

print(f"\nTotal: ${total:.2f}")

confirm = input("Do you want to proceed to checkout? (yes/no):


").lower()

if confirm == "yes":

print("\nThank you for shopping! Your order has been


placed.")

shopping_cart.clear()

else:

print("\nCheckout canceled.")

elif choice == "6":

# Exit the program

print("\nExiting the Shopping Cart System. Goodbye!")

break

else:

print("Invalid choice. Please try again.")

10

You might also like