Project: The Smart Shopping Cart

Skills Challenge: Parts 1-4

This project is a cumulative test of your journey so far. You will need to apply:

  • Part 1 & 2: Input handling, float conversion, and budget logic.
  • Part 3: Controlling program flow with while loops.
  • Part 4: Dynamic data storage with Lists and logic encapsulation with Functions.

In this project, you will build a shopping program. Unlike previous exercises, this requires maintaining a persistent state (your money and your cart) throughout a loop until the user decides to quit.

Phase 1: The Wallet Logic

First, we need to handle the money. Your program should ensure that the user cannot buy something they cannot afford.

Programming exercise: The Wallet
Points: 0/1

Write a program that first asks for a Starting Budget.

Then, create a loop that prints the current budget and asks for a command:

  • 1: Add item
  • 0: Quit

If the user selects 1, ask for the Price of the item.

  • If the price is greater than the budget, print "Too expensive!"
  • Otherwise, subtract the price from the budget and print "Item added."
Sample output
How much money do you have? 50.0 Current Budget: 50.0 1. Add item 0. Quit Select: 1 Price: 60 Too expensive! Current Budget: 50.0 Select: 1 Price: 10 Item added. Current Budget: 40.0 Select: 0 Bye!
Money often has decimals. Use float() to convert the input so you can perform math correctly:
budget = float(input("Budget: "))

Phase 2: The List Storage

Now, let's keep track of what we are buying using a list. We need to update our menu so the user can actually see their cart.

Programming exercise: Adding Items
Points: 0/1

Create an empty list called cart at the start of your program.

Update your menu to show three options: 1. Add, 2. View, and 0. Quit.

When the user adds an item:

  1. Ask for the Item Name (e.g., "Bread").
  2. If they can afford it, append that name to your cart list.

When the user selects 2, simply print the cart list.

Sample output
1. Add 2. View 0. Quit Select: 1 Item name: Coffee Price: 5.0 Added Coffee. Select: 2 Your cart: ['Coffee']

Phase 3: Clean Formatting & Functions

In Part 4, we learned that global code can become messy. Let's move our display logic into a function and format the list so it doesn't look like a raw Python list with brackets.

Programming exercise: Professional Cart
Points: 0/2

Step 1 (Formatting): When adding an item, use an f-string to store the name and price together in the list as one string, for example: "Apple ($2.5)".

Step 2 (The Function): Create a function print_status(budget, cart). Inside this function, use a while loop and an index variable to print the cart items line-by-line.

def print_status(budget, cart):
    print(f"Current Budget: {budget}")
    if len(cart) == 0:
        print("Cart is empty")
    else:
        # Use a while loop to print each item nicely
Since we aren't using for loops yet, use a counter:
i = 0
while i < len(cart):
    print(f"- {cart[i]}")
    i += 1

Phase 4: Changing Your Mind (Bonus)

Programming exercise: The Refund
Points: 0/1

What if the user accidentally adds the wrong item? Add one last menu option: 3. Remove last item.

When selected, use the pop() method to remove the last item from the cart list.