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!
==========================
# Note: "pass" doesn't do anything, it's just a placeholder.
# Delete pass when you add your code.
# Display all items in cart with prices and total
def display_cart(cart):
# TODO: Show numbered list of items
# TODO: Calculate and show total
pass
# Add item to cart or update if exists
def add_to_cart(cart, item, price):
# TODO: Check if item exists
# TODO: Add or update price
# TODO: Return True if successful
pass
# Remove item by position number
def remove_from_cart(cart, item_number):
# TODO: Convert display number to actual position
# TODO: Remove item and return its price
pass
# Calculate total price of all items
def calculate_total(cart):
# TODO: Sum all prices in cart
pass
# Main program
def main():
cart = {} # Empty dictionary to store items and prices
while True:
# TODO: Show menu
# TODO: Get user choice
# TODO: Handle each menu option
pass
main()