Follow along with the slides, then work through the lab below.
Two 20-minute labs on event bookings. Extensions are optional.
Lab 1: Values and conversion
Getting started (2 minutes)
- Open the lab notebook in Colab and save your own copy.
- Click the play button beside a code cell to run it. Work from top to bottom.
- Double-click text to edit an answer. Click outside when done.
Try the cell below.
greeting = "Ready to book!"greeting1. Predict, run, explain (3 minutes)
Predict each value and type: string (text), integer or float (decimal number). Then run the cells and record the results.
| Expression | Predicted value | Predicted type | Observed value |
|---|---|---|---|
"6" + "0" | |||
6 + 0 | |||
60 - 25 | |||
2.50 * 2 |
"6" + "0"6 + 060 - 252.50 * 2Answer
| Expression | Predicted value | Predicted type | Observed value |
|---|---|---|---|
"6" + "0" | "60" | string | '60' |
6 + 0 | 6 | integer | 6 |
60 - 25 | 35 | integer | 35 |
2.50 * 2 | 5.0 | float | 5.0 |
type(value) returns str for text, int for integers or float for decimal numbers. Use it to check your predictions.
Why do "6" + "0" and 6 + 0 differ?
type("6" + "0")Answer
"6" + "0" joins text to make "60". 6 + 0 adds numbers to give 6.
2. Plan an event (7 minutes)
There are 60 seats, 25 bookings and tickets cost £2.50.
- Set
capacity,bookedandticket_pricein the first cell. - Calculate
spacesandincomein the next cell. - Run both. Expect 35 spaces and 62.5 in income (£62.50).
# Define capacity, booked and ticket_price here.# Calculate spaces and income here.
print(spaces)print(income)Answer
capacity = 60booked = 25ticket_price = 2.50spaces = capacity - bookedincome = booked * ticket_priceprint(spaces)print(income)Predict the results for 30 bookings. Set booked = 30 and run only that cell, then the cell below.
Why are the results unchanged? Rerun the calculation cell to update them.
Restore booked = 25 and rerun both cells before continuing.
print(spaces)print(income)Answer
With 30 bookings, expect 30 spaces and £75.00.
spaces and income keep their old values until you rerun the calculations.
3. Read a sign-up form (5 minutes)
The form gives "25" bookings. One more person books.
Convert booked_text with int, add one and store it as booked. Calculate and display spaces too. Expect 26 bookings and 34 spaces.
capacity = 60booked_text = "25"# Update booked, calculate spaces, and display both.Answer
capacity = 60booked_text = "25"booked = int(booked_text) + 1spaces = capacity - bookedprint(booked)print(spaces)Predict booked_text + "1", then run it. Why is the result wrong for counting bookings?
booked_text + "1"Answer
booked_text + "1" joins strings to give "251". Convert to an integer before adding one to get 26.
Lab 1 check (1 minute)
Explain why you need to:
- Rerun calculations after changing
booked. - Convert form text before adding one.
Answer
- Calculations must run again to use the new value of
booked. intconverts text so you can add one numerically.
Extension A: A price from the form
Convert "2.50" with float. Calculate income for the 26 bookings from task 3. Expect 65.0 (£65.00).
Why would int fail here?
price_text = "2.50"# Use booked from task 3 to calculate income.Answer
price_text = "2.50"income = booked * float(price_text)incomefloat("2.50") gives 2.5. For 26 tickets, income is 65.0.
int("2.50") raises a ValueError because the text contains a decimal point.
Lab 2: Decisions and debugging
You can start here without finishing Lab 1.
4. Choose a message (6 minutes)
Use if, elif and else to set message. Assume bookings are non-negative integers.
| Bookings | Message |
|---|---|
| Above capacity | Waiting list |
| At capacity | Full |
| Below capacity | Spaces available |
Predict the messages in the table below. Write your code, then change booked to test each count.
capacity = 60booked = 25# Choose message here.
print(message)Answer
capacity = 60booked = 25if booked > capacity: message = "Waiting list"elif booked == capacity: message = "Full"else: message = "Spaces available"print(message)| Bookings | Expected message | Actual message |
|---|---|---|
| 25 | ||
| 60 | ||
| 61 |
Why is testing exactly 60 important?
Answer
| Bookings | Expected message | Actual message |
|---|---|---|
| 25 | Spaces available | Spaces available |
| 60 | Full | Full |
| 61 | Waiting list | Waiting list |
Testing 60 checks equality. Using >= in the first branch would wrongly give Waiting list.
5. Fix two bugs (5 minutes)
Run each cell and read the last line of its error. Fill in the table, then fix and rerun the code. Expect 34 spaces and 26 bookings.
capacity = 60booked_text = "26"capacity - booked_textbooking_count = 26booking_cout| Bug | Error type | Cause | What I changed |
|---|---|---|---|
| Subtraction | |||
| Booking count |
Answer
capacity = 60booked_text = "26"capacity - int(booked_text)booking_count = 26booking_count| Bug | Error type | Cause | What I changed |
|---|---|---|---|
| Subtraction | TypeError | Subtracting a string from an integer | Converted booked_text with int before subtracting |
| Booking count | NameError | booking_cout has not been defined | Corrected the spelling to booking_count |
6. Use the documentation (5 minutes)
Find the occupancy percentage for 25 bookings in a 60-seat room.
- Calculate
percentage = booked / capacity * 100. - Read the documentation for
round. Round to one decimal place and store it asoccupancy. - Display the result. Expect 41.7.
capacity = 60booked = 25# Calculate percentage, then occupancy.Answer
capacity = 60booked = 25percentage = booked / capacity * 100occupancy = round(percentage, 1)print(occupancy)print(round(percentage))Compare round(percentage, 1) with round(percentage). What changes?
Optional: test 30 and 60 bookings. Expect 50.0 and 100.0 to one decimal place.
Answer
The second input, ndigits, controls decimal places. round(percentage, 1) gives the float 41.7. round(percentage) gives the integer 42.
For 30 bookings, the one-decimal result is 50.0. For 60 bookings, it is 100.0.
Lab 2 check (2 minutes)
Check your three messages, bug fixes and rounded percentage.
For a fresh check, restart the session from Colab’s Runtime menu. Use the play buttons to run your completed cells in order. If Lab 1 is unfinished, start at Lab 2.
Answer
- Messages:
Spaces available,Full,Waiting list. - Fixes: convert the text with
intand correct the variable name. round(percentage, 1)gives one decimal place.
Extension B: Can the whole group fit?
Set capacity = 60 and booked = 56. Use if and else to set group_message to Booking accepted if a group fits, or Not enough spaces otherwise.
Predict and test tickets_requested values of 3, 4 and 5. Keep booked unchanged.
# Define capacity, booked and tickets_requested, then choose group_message.Why should a request for exactly four tickets be accepted?
Answer
capacity = 60booked = 56tickets_requested = 4spaces = capacity - bookedif tickets_requested <= spaces: group_message = "Booking accepted"else: group_message = "Not enough spaces"print(group_message)Requests for 3 and 4 tickets give Booking accepted. 5 gives Not enough spaces.
Four tickets fill the remaining spaces exactly, so use <=.
Extension C: A bug without an error message
Change > to >= in task 4. Which test catches the mistake? Try it, then restore >.
Answer
At 60 bookings, >= gives Waiting list instead of Full. The code runs but gives the wrong answer. Restore >.