Course 1.1Lesson 5 of 5
In this project we write a tiny shop. Two dictionaries hold the prices and the stock, and a list is the shopping basket. The program goes through the basket, refuses items that the shop does not sell, refuses items that are out of stock, takes one item off the shelf for each sale, and adds the price to the total. We plan the rules and the expected answers first, build the program in steps, test the unusual cases, and extend the shop with discounts and a receipt that counts how many of each item were bought.
Try the code and the simulations beside the lesson.
After this lesson you can
A small shop sells pens, books, bags and rulers. The shop has a price for each item, and a stock: how many are on the shelf. A customer brings a basket, a list of the items that the customer wants. The program should
Two dictionaries, with the same keys, and a list:
prices: pen 10, book 80, bag 450, ruler 15.stock: pen 20, book 5, bag 0, ruler 12.basket: pen, pen, book, bag, eraser, ruler.Work out the answer by hand first.
| Item | What happens | Total so far |
|---|---|---|
| pen | sold, 10 | 10 |
| pen | sold, 10 | 20 |
| book | sold, 80 | 100 |
| bag | stock is 0: out of stock | 100 |
| eraser | not in the price list: not sold | 100 |
| ruler | sold, 15 | 115 |
The total to pay must be 115. The stock must become: pen 18, book 4, bag 0, ruler 11.
This is a general idea: put the check that protects the later lines first.
Notice also that we use the dictionaries as tables of facts (prices, stock), and a list as an ordered sequence of events (the basket). The data decides which one to use.
The example had one of each case. Now change the data and work out the answers by hand first.
| Test | Expected |
|---|---|
| an empty basket | total 0, stock unchanged |
| 6 books (only 5 in stock) | five sold, the sixth refused, total 400 |
| only "eraser" | nothing sold, total 0 |
| the same item twice in a row | stock goes down by 2 |
The second row is the edge: the stock goes to 0 in the middle of the basket. The elif stock[item] == 0 must then refuse the sixth book. Does it?
The order of the tests matters: first is the item sold at all, then is there enough of it.
Pick one or two, and do them without looking at a solution. Write the expected answers first.
pen x 2, book x 1, and so on.{"pen": 3, "book": 1}, where the value is the number wanted. What changes in the loop?restock(stock, item, amount) that adds to the stock of an item, or makes a new item.KeyError.stock[item] == 0 and forgetting a negative stock. Use <= 0 if a stock can go below 0.KeyError.Start with two quick questions. Each New numbers gives you a fresh one, so you can practise as long as you like.
Then the labs and the problems:
Try each one on your own first. If you are stuck, open a hint. Then check with Show the answer.
item not in prices come first in the if?prices?Every lesson is open to read. Enrolling also lets you:
Log in to join the discussion.
Loading…
This lesson is free to read. Enroll free to keep your place and take the tests.