Build a shopping cart application that tracks items, their prices, and calculates totals. This assignment will practice dictionaries, lists, functions, and user interaction.
Your shopping cart app must track both items and their costs using dictionaries.
Write the following functions:
display_cart(cart)
- Shows all items with prices and total
# Example output:
Shopping Cart:
1. Milk - 3.99
2. Bread - 2.49
3. Eggs - 4.99
Total: $11.47
add_to_cart(cart, item, price)
- Adds an item with its price
remove_from_cart(cart, item_number)
- Removes item by position
calculate_total(cart)
- Returns the total cost of all items
Create a menu-driven program:
=== SHOPPING CART ===
Current total: $0.00
1. View cart
2. Add item
3. Remove item
4. Clear cart
5. Checkout
Enter choice:
Requirements:
=== SHOPPING CART ===
1. View cart
2. Add item
3. Remove item
4. Clear cart
5. Checkout
Enter choice: 2
Enter item name: Milk
Enter price: 3.99
Added Milk (3.99) to cart!
Enter choice: 2
Enter item name: Bread
Enter price: 2.49
Added Bread (2.49) to cart!
Enter choice: 1
Shopping Cart:
1. Milk - 3.99
2. Bread - 2.49
Total: 6.48
Enter choice: 2
Enter item name: Milk
Milk is already in cart at 3.99
Enter new price (or 0 to cancel): 4.29
Updated Milk to 4.29
Enter choice: 5
Final Shopping Cart:
1. Milk - 4.29
2. Bread - 2.49
Total: $6.78
Thank you for shopping!
Milk - 3.99 x 2 = 7.98
calculate_total()
to handle quantitiescatalog = {
"milk": 3.99,
"bread": 2.49,
"eggs": 4.99,
# ... more items
}
Create a generate_receipt(cart)
function that produces:
========== RECEIPT =======
Date: 2024-01-15
Milk................. 3.99
Bread................ 2.49
Eggs (x2)............ 9.98
Subtotal: 16.46
Tax (8%): 1.32
--------------------------
TOTAL: 17.78
Thank you for shopping!
==========================
from datetime import datetime # For print_receipt
# Display all items in cart with prices and total
def display_cart(cart):
i = 1
for item in cart:
print(f"{i}. {item} - ${cart[item]:.2f}")
i += 1
# Add item to cart or update if exists
def add_to_cart(cart, item, price):
cart[item] = price
return cart
# Remove item by position number
def remove_from_cart(cart, item_number):
i = 1
for item in cart:
if i == item_number:
cart.pop(item)
return cart
print("WARNING: No item with number {i} found!")
return cart
# Calculate total price of all items
def calculate_total(cart):
total = 0
for item in cart:
total += cart[item]
return total
# Helper function for nicely printing lines in receipt
def print_fill(left_side, right_side, filler, line_len):
middle = ""
while len(f"{left_side}{middle}{right_side}") < line_len:
middle += filler
print(f"{left_side}{middle}{right_side}")
def print_receipt(cart):
print("========== RECEIPT =======")
print(f"Date: {datetime.now().date().isoformat()}")
print("")
for item in cart:
print_fill(item, f" ${cart[item]:.2f}", ".", 26)
print("")
subtotal = calculate_total(cart)
tax = 0.08 * subtotal
print_fill("Subtotal:", f" ${subtotal}", " ", 26)
print_fill("Tax (8%):", f" ${tax:.2f}", " ", 26)
print("--------------------------")
total = subtotal + tax
print_fill("Total:", f" ${total:.2f}", " ", 26)
print("==========================")
# # Main program
cart = {} # Empty dictionary to store items and prices
while True:
print("Menu:")
print("1. Display Cart")
print("2. Add to Cart")
print("3. Remove from Cart")
print("4. Get Total")
print("5. Checkout")
choice = input("Enter choice: ")
if choice == "1":
display_cart(cart)
elif choice == "2":
item = input("Item: ")
price = float(input("Price: "))
cart = add_to_cart(cart, item, price)
elif choice == "3":
item_number = int(input("Item Number: "))
cart = remove_from_cart(cart, item_number)
elif choice == "4":
total = calculate_total(cart)
print(f"Total: {total:.2f}")
elif choice == "5":
print_receipt(cart)
break
else:
print(f"Invalid option: {choice}")
Menu: 1. Display Cart 2. Add to Cart 3. Remove from Cart 4. Get Total 5. Checkout Enter choice: 2 Item: Cheese Price: 3.99 Menu: 1. Display Cart 2. Add to Cart 3. Remove from Cart 4. Get Total 5. Checkout Enter choice: 2 Item: Milk Price: 4.49 Menu: 1. Display Cart 2. Add to Cart 3. Remove from Cart 4. Get Total 5. Checkout Enter choice: 2 Item: Eggs Price: 14.89 Menu: 1. Display Cart 2. Add to Cart 3. Remove from Cart 4. Get Total 5. Checkout Enter choice: 1 1. Cheese - $3.99 2. Milk - $4.49 3. Eggs - $14.89 Menu: 1. Display Cart 2. Add to Cart 3. Remove from Cart 4. Get Total 5. Checkout Enter choice: 4 Total: 23.37 Menu: 1. Display Cart 2. Add to Cart 3. Remove from Cart 4. Get Total 5. Checkout Enter choice: 5 ========== RECEIPT ======= Date: 2025-08-01 Cheese.............. $3.99 Milk................ $4.49 Eggs............... $14.89 Subtotal: $23.37 Tax (8%): $18.70 -------------------------- Total: $42.07 ==========================