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

Free lesson

ESP task 4a (1 of 5): Build the solution in small steps

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 ways to build a wall

Builder A puts down all the bricks for the whole house and then checks. A wall that is not straight is found on the last day.

Builder B puts down one row of bricks and checks it. Then the next row, and checks again. A wall that is not straight is found in a minute.

Be Builder B. In the build task, a small working step is always better than a big broken one.

Key words

WordWhat it meansExample
BuildWrite the code that makes the solution.write book()
PrototypeAn early working version that does the main job.it books a court
CommentA note in the code for people. Python ignores it.# check the hour first
Naming conventionA rule for how you write names, so they are easy to read.price_for, not pf
VersionA saved copy of your work at one moment.booking_v2.py
Version controlA tool that keeps every saved version of your work.Git
Must-haveA part the tool cannot work without.book a court
Should-haveAn important part, but the tool still works without it.cancel a booking
ModuleA part of the program that does one job and can be tested alone.the pricing part

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

The small-step build loop

flowchart TD
  a([Pick the next must-have]) --> b[Write a little code]
  b --> c[Run it]
  c --> d{Does it work?}
  d -->|no| e[Fix it]
  e --> c
  d -->|yes| f[Save a version and note it]
  f --> g{More must-haves?}
  g -->|yes| a
  g -->|no| h([Add should-haves if time])

Step 1: a booking with no checks

Code: python
bookings = []


def book(bookings, member, court, hour):
    bookings.append({"member": member, "court": court, "hour": hour})
    return True


print(book(bookings, "Sam", 1, 10))
print(bookings)

# This works but it books anything.
# Book Sam on court 1 at hour 10 again. What happens?

Make a free account to save your work and track your progress.

Step 2: one check stops a double booking

Code: python
bookings = []


def is_free(bookings, court, hour):
    for one in bookings:
        if one["court"] == court and one["hour"] == hour:
            return False
    return True


def book(bookings, member, court, hour):
    if not is_free(bookings, court, hour):
        return False
    bookings.append({"member": member, "court": court, "hour": hour})
    return True


print(book(bookings, "Sam", 1, 10))
print(book(bookings, "Aisha", 1, 10))
print(len(bookings))

Make a free account to save your work and track your progress.

One check at a time

Tip

Step 1 booked anything. Step 2 added one check. The next step adds the hour check. The next adds the court check. After every step the code runs. That is how you stay sure the code works.

Match the habit to the reason

Have a go

Loading…

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

Check: good building habits

Have a go

Check: good building habits

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

Stage 4a: build book and cancel

This is stage 4a of the Hillside practice project. Build book and cancel. is_free is finished for you. Work in small steps: first make book add a booking, then add one check at a time, then write cancel. Run the tests after each step. Press Run Tests to check your work.

Code: python
OPEN_HOUR = 9
LAST_HOUR = 20


def is_free(bookings, court, hour):
    # This one is finished. Use it.
    for one in bookings:
        if one["court"] == court and one["hour"] == hour:
            return False
    return True


def book(bookings, member, court, hour):
    # Add a booking and give back True.
    # Give back False and add nothing if:
    #   the hour is before OPEN_HOUR or after LAST_HOUR,
    #   the court is not 1 or 2,
    #   or the court is not free at that hour.
    # A booking is a dictionary: {"member": ..., "court": ..., "hour": ...}
    return None


def cancel(bookings, member, court, hour):
    # Remove that member's booking and give back True.
    # Give back False if there is no such booking for that member.
    return None

Make a free account to save your work, or press Start lab to work through the whole lesson.

Save a version after each working step

After each working step, save a copy with a new number: booking_v1.py, booking_v2.py. Write one line in a log: what you added and that the test passed.

The log is evidence (proof) of how you worked. Ask your lecturer if your course wants you to use a version control tool or just numbered copies.

Key words crossword

Have a go

Loading…

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

Final check

Have a go

Final check

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.