CodeBlanks Logo Icon CodeBlanks

Cart design

Design the shopping cart class skeleton.

Project: Shopping cart

In this 5-lesson project you'll build a shopping cart. Each lesson adds one piece, and by the final lesson you'll have a complete app.

You'll use what you learned before:

  • lists and variables
  • output with print
  • for loops
  • decisions with if/elif

You'll also learn list methods (append), calculating totals and updating quantities. Ready? Let's start.

The cart flow

The shopping cart works in three steps:

  1. Adding: an item is added to the cart.
  2. Listing: the items in the cart are shown.
  3. Total: the prices are summed.

In the first lesson we'll just define an empty cart (a list) and print it. The next lessons add item adding, total calculation and quantity updates.

Practice Print the empty cart

Define the empty cart with cart = []. Print it with print(cart).

Starter code

main.py
# Define the cart

Sample solution

main.py
cart = []
print(cart)

Expected output

[]

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 What does a shopping cart need first?

  • A database
  • A class
  • A list to hold items Correct answer
  • A file

Explanation: A cart uses a list to hold items.

2 Define an empty cart

Question
cart = ____
print(cart)
Answer
cart = []
print(cart)

Explanation: cart = [] defines an empty list (the cart).

3 Match the cart steps

Term Meaning
Adding Item is placed in the cart
Listing Items are shown
Total Prices are summed
cart Cart variable

All lessons in Beginner