The Shopping Cart
Welcome to the last lesson of the Python course! In it, you'll put together what you've learned and build one useful program step by step: the receipt for an order from an online grocery shop. When you finish, your program will print this:
Can't add Eggs: the quantity must be at least 1
Can't add Tea: invalid literal for int() with base 10: 'three'
RECEIPT
Chocolate: 3 x $5 = $15
Coffee: 2 x $7 = $14
Milk: 6 x $2 = $12
Cheese: 1 x $7 = $7
Bread: 2 x $3 = $6
Total: $54
Delivery: free
To pay: $54The program reads what the customer added on the shop's website, skips the lines that make no sense, merges the same items, works out the price of each line and of the whole order, puts the most expensive lines first and decides whether the delivery is free. You'll use a class, lists, loops, functions, conditions, text methods, a list comprehension, sorting and exceptions. Each exercise continues where the previous one ended (the editor starts with its finished code) and adds one piece.
The data. At the top of the program is the class Item. Its __init__ gives every item three attributes: name, price (the price of one piece, in whole dollars) and quantity (how many pieces the customer wants). Under the class is the cart, cart, a list of Item objects. The class has to come first: Python must know Item before the cart can create items. A dictionary could keep an item's name, price and quantity together too, but a class can do more: in the next exercise, Item will learn to work out and show its own line of the receipt.
The plan. The rest of the program is split into functions, each with one job. So far, there's one: print_receipt(items), which prints the receipt for the list of items it gets as an argument. For now, it prints only the heading. At the very bottom, the program calls it with our cart. That's the usual shape of a program: the data at the top, then the functions, and at the end the code that uses them. Inside the function, work with the parameter items, not with cart. Then the function prints the receipt for whatever list it gets, not just for this one cart.
A reminder of how to go through a list of objects: the loop variable holds one object at a time, and you read its attributes after a dot. In an f-string, they need no quotes, unlike the keys of a dictionary:
class Book:
def __init__(self, title, pages):
self.title = title
self.pages = pages
books = [Book("The Hobbit", 310), Book("Matilda", 240)]
for book in books:
print(f"{book.title}: {book.pages} pages")The Hobbit: 310 pages
Matilda: 240 pagesOther data. As in the checkpoints, each exercise also runs your program with other quantities and prices in the cart. So leave the lines of the cart as they are, and let the program take every number from the items: don't write the numbers into your code.
Instructions
- 1
In
print_receipt(), under the line that prints the heading, write aforloop that goes throughitemsand prints the name of each item. - 2
Complete the line in the loop so that it also shows the quantity and the price of one piece. Use an f-string. The line with the bread will be
Bread: 2 x $3. The$is ordinary text in the f-string: write it right before the braces with the price, without a space.