Warwick AI
Workshop 1
workshop-1

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!"
greeting

1. 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.

ExpressionPredicted valuePredicted typeObserved value
"6" + "0"
6 + 0
60 - 25
2.50 * 2
"6" + "0"
6 + 0
60 - 25
2.50 * 2
Answer
ExpressionPredicted valuePredicted typeObserved value
"6" + "0""60"string'60'
6 + 06integer6
60 - 2535integer35
2.50 * 25.0float5.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.

  1. Set capacity, booked and ticket_price in the first cell.
  2. Calculate spaces and income in the next cell.
  3. 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 = 60
booked = 25
ticket_price = 2.50
spaces = capacity - booked
income = booked * ticket_price
print(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 = 60
booked_text = "25"
# Update booked, calculate spaces, and display both.
Answer
capacity = 60
booked_text = "25"
booked = int(booked_text) + 1
spaces = capacity - booked
print(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.
  • int converts 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)
income

float("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.

BookingsMessage
Above capacityWaiting list
At capacityFull
Below capacitySpaces available

Predict the messages in the table below. Write your code, then change booked to test each count.

capacity = 60
booked = 25
# Choose message here.
print(message)
Answer
capacity = 60
booked = 25
if booked > capacity:
message = "Waiting list"
elif booked == capacity:
message = "Full"
else:
message = "Spaces available"
print(message)
BookingsExpected messageActual message
25
60
61

Why is testing exactly 60 important?

Answer
BookingsExpected messageActual message
25Spaces availableSpaces available
60FullFull
61Waiting listWaiting 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 = 60
booked_text = "26"
capacity - booked_text
booking_count = 26
booking_cout
BugError typeCauseWhat I changed
Subtraction
Booking count
Answer
capacity = 60
booked_text = "26"
capacity - int(booked_text)
booking_count = 26
booking_count
BugError typeCauseWhat I changed
SubtractionTypeErrorSubtracting a string from an integerConverted booked_text with int before subtracting
Booking countNameErrorbooking_cout has not been definedCorrected the spelling to booking_count

6. Use the documentation (5 minutes)

Find the occupancy percentage for 25 bookings in a 60-seat room.

  1. Calculate percentage = booked / capacity * 100.
  2. Read the documentation for round. Round to one decimal place and store it as occupancy.
  3. Display the result. Expect 41.7.
capacity = 60
booked = 25
# Calculate percentage, then occupancy.
Answer
capacity = 60
booked = 25
percentage = booked / capacity * 100
occupancy = 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 int and 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 = 60
booked = 56
tickets_requested = 4
spaces = capacity - booked
if 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 >.