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

Preview

Judging a solution

This is the first page of the lesson. Read it and have a go at the activities. The rest is in the full lesson.

45 minutes

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.

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")))

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

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.

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.

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

Your answers are marked here and are not kept. A college account keeps every student’s marks.

The lesson goes on

4 more parts of this lesson are in the full curriculum, with the tasks, the final check and the labs. A college account gives your students all of it.