Two answers to one problem
A corner shop (a small local shop) wants a program. It must show the price of an item and add the delivery fee. Two students each write one.
Both programs give the right answer today. So which is better? This lesson gives you a way to decide and to say why.
Key words
| Word | What it means | Example |
|---|---|---|
| Requirement | Something the program must do. | |
| Suitable | Right for the job and its requirements. | |
| Constant | A value that never changes while the program runs. Its name is in capitals. | DELIVERY_FEE |
| Maintainability | How easy it is to fix or change the code later. | |
| Presentation | How the output looks to the user. | £2.50 and not 2.5 |
| Data type | The kind of thing a value is. | string, integer |
| Data structure | A way to store a group of values. | list, dictionary |
| Efficient | Uses little time and little memory. | |
| Variable | A name that holds a value. | |
| Dictionary | A group of pairs. Each pair is a key and a value. You find the value by its key. | {"tea": 1.2} |
| Function | A named piece of code that does a job. |
The cells students fill in are left blank in this preview.
Solution A
item = "tea"
if item == "tea":
price = 1.2
elif item == "coffee":
price = 2.5
elif item == "cake":
price = 3.0
if price < 30:
price = price + 3.5
print(price)
# Run it. Then read the code and look for problems.
Make a free account to save your work and track your progress.
Solution B
PRICES = {"tea": 1.2, "coffee": 2.5, "cake": 3.0}
DELIVERY_FEE = 3.5
FREE_DELIVERY_FROM = 30.0
def order_total(item):
total = PRICES[item]
if total < FREE_DELIVERY_FROM:
total = total + DELIVERY_FEE
return total
# {:.2f} shows a number with 2 decimal places.
print("Total: £{:.2f}".format(order_total("tea")))
Make a free account to save your work and track your progress.
Give a reason for every judgement
Do not say "B is nicer". Give reasons. Use a list of questions. Each question looks at one part of the code.
A good judgement names the part, says what you see, and says why it matters. For example: "B uses a dictionary, so adding an item is one line."
The judging questions
| Question | What to look for |
|---|---|
| Does it meet the requirements? | Does it do everything the user asked, and nothing wrong? |
| Is it efficient in time? | Does it do more steps than it needs? |
| Is it efficient in storage? | Does it keep more data in memory than it needs? |
| Are the data structures right? | A list for items in order. A dictionary for finding a value by its name |
| Are the data types, variables and constants right? | The right type for each value. Fixed values named once as constants |
| Is the presentation clear? | Does the output look right: pound sign, two decimals, a message |
| Is it maintainable? | Meaningful names, short functions, no repeated code |
The cells students fill in are left blank in this preview.
A judgement of A and B
| Question | Solution A | Solution B |
|---|---|---|
| Meets the requirements | Yes for three items | Yes for three items |
| Adding a new item | Needs a new elif | One new line in PRICES |
| Fees and limits | The numbers 3.5 and 30 are typed in the code | Named once as constants |
| Presentation | Prints 4.7 | Prints £4.70 |
| Maintainability | Weak: the fee may be typed in many places | Strong: change the constant once |
| Verdict | Works, but hard to change later | More suitable |
The cells students fill in are left blank in this preview.
Which question does the problem break?
Have a go
Loading…
Your answers are marked here and are not kept. A college account keeps every student’s marks.
Try it: the wrong data type
phone_text = "07700900123"
phone_number = int(phone_text)
print(phone_text)
print(phone_number)
# The integer lost its first 0.
# A phone number is not for adding up, so it should be a string.
Make a free account to save your work and track your progress.
Quick check: which is more suitable?
Have a go
Quick check: which is more suitable?
4 questionsWatch: two solutions judged
Press Play on the short film below. It runs Solution A and Solution B, then judges them with the questions from this lesson. Listen for the reason it gives for each judgement.
Film: judging a solution
Judging a solution
Every scene, as text
- Two students solve one problem: show the price of an item and add the delivery fee. Solution A checks the item with if and elif: tea is 1.2, less than 30, so 3.5 is added and print(price) prints 4.7.
- Solution B looks the price up in the dictionary PRICES and adds DELIVERY_FEE in the function order_total, so it prints Total: £4.70. Both meet the requirement, for three items.
- Myth (false): a program that gives the right answer is always suitable. Both solutions give the right answer today. To sell a sandwich at 4.0, Solution A needs a new elif and a new price line, while Solution B needs one new pair in PRICES. A works, but it is harder to change.
- Fees and limits. Solution A types the numbers 30 and 3.5 into its code, so if the fee is typed in many places, a new fee means finding every one. Solution B names them once as constants in capitals, DELIVERY_FEE and FREE_DELIVERY_FROM, so a new fee is one change.
- Data types: the right type for each value. phone_text is set to the string "07700900123". int(phone_text) turns it into an integer, which keeps no leading 0, so print(phone_number) prints 7700900123. A phone number is not for adding up, so it should be a string.
- Presentation: how the output looks to the user. Both outputs are the same total, tea 1.2 plus the 3.5 fee. A's output prints it as it is, 4.7. B's output uses the format code {:.2f}, which shows the number with two decimal places, so it prints Total: £4.70. The verdict: A works, but is hard to change later. B is more suitable.
Your turn: build solution B
Build the better solution. The constants are at the top. order_total(items) adds up the prices of a list of items. If the total is below FREE_DELIVERY_FROM, it adds DELIVERY_FEE. A total exactly equal to it pays no fee. Use the constants and do not type their numbers again. cheapest_item(prices) gives back the name of the cheapest item. price_label(amount) gives back text such as "£2.50", with format. Press Run Tests to check.
PRICES = {"tea": 1.2, "coffee": 2.5, "cake": 3.0, "sandwich": 4.0}
DELIVERY_FEE = 3.5
FREE_DELIVERY_FROM = 30.0
def order_total(items):
# items is a list of item names, such as ["tea", "cake"].
# Add up their prices from PRICES.
# If the total is less than FREE_DELIVERY_FROM, add DELIVERY_FEE.
# Use the constants. Do not type 3.5 or 30.0 again.
return None
def cheapest_item(prices):
# prices is a dictionary like PRICES. Give back the NAME of the cheapest item.
return None
def price_label(amount):
# Give back text for a price with a pound sign and two decimal places.
# price_label(2.5) gives back "£2.50". Use format().
# The pound sign is just a character in the text.
return None
Make a free account to save your work, or press Start lab to work through the whole lesson.
Your turn: write a judgement
Look again at Solution A and Solution B. Write 5 to 8 sentences to judge which is more suitable for the corner shop. Name at least three of the judging questions. For each one, say what you see in the code and why it matters. End with a clear verdict. Use plain words and give a reason for every claim.
A teacher marks this in a college. Nothing you write here is kept: it goes when you leave the page.
Make a free account to save your work, or press Start lab to work through the whole lesson.
Key words crossword
Have a go
Loading…
Your answers are marked here and are not kept. A college account keeps every student’s marks.
Exit quiz
Have a go