Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

2.9 (3 of 3): Judging a solution

The whole lesson, free. Read it and have a go at the activities. To work the tasks and labs in the real lesson, press Start lab, with no account needed.

45 minutes

A quick check that you are not a bot, then your own lab opens on the interactive lesson.

Already have an account? Sign in

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

WordWhat it meansExample
RequirementSomething the program must do.
SuitableRight for the job and its requirements.
ConstantA value that never changes while the program runs. Its name is in capitals.DELIVERY_FEE
MaintainabilityHow easy it is to fix or change the code later.
PresentationHow the output looks to the user.£2.50 and not 2.5
Data typeThe kind of thing a value is.string, integer
Data structureA way to store a group of values.list, dictionary
EfficientUses little time and little memory.
VariableA name that holds a value.
DictionaryA group of pairs. Each pair is a key and a value. You find the value by its key.{"tea": 1.2}
FunctionA named piece of code that does a job.

The cells students fill in are left blank in this preview.

Solution A

Code: python
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

Code: python
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

QuestionWhat 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

QuestionSolution ASolution B
Meets the requirementsYes for three itemsYes for three items
Adding a new itemNeeds a new elifOne new line in PRICES
Fees and limitsThe numbers 3.5 and 30 are typed in the codeNamed once as constants
PresentationPrints 4.7Prints £4.70
MaintainabilityWeak: the fee may be typed in many placesStrong: change the constant once
VerdictWorks, but hard to change laterMore 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

Code: python
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 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Watch: 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

  1. 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.
  2. 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.
  3. 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.
  4. 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.
  5. 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.
  6. 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.

Code: python
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

Exit quiz

5 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Teach this lesson with your class

On a college account your students work the tasks and the labs as well, and every mark is kept for you.