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.
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")))
How to judge a solution
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.
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.
Quick check: which is more suitable?
Have a go
Quick check: which is more suitable?
4 questionsYour answers are marked here and are not kept. A college account keeps every student’s marks.