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
| Word | What it means | Example |
|---|---|---|
| Build | Write the code that makes the solution. | write book() |
| Prototype | An early working version that does the main job. | it books a court |
| Comment | A note in the code for people. Python ignores it. | # check the hour first |
| Naming convention | A rule for how you write names, so they are easy to read. | price_for, not pf |
| Version | A saved copy of your work at one moment. | booking_v2.py |
| Version control | A tool that keeps every saved version of your work. | Git |
| Must-have | A part the tool cannot work without. | book a court |
| Should-have | An important part, but the tool still works without it. | cancel a booking |
| Module | A 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
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
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 questionsStage 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.
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