Cart design
- Python course
- Beginner
- Lesson 48/116
- 6 min
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:
- Adding: an item is added to the cart.
- Listing: the items in the cart are shown.
- 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
# Define the cartSample solution
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
cart = ____
print(cart)
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 |