Curriculum

Curriculum

Run Your First Function in Python

Scenario

Save the example as booking.py and run python3 booking.py.

Selected choicePick a choice below
Result—
Python
# booking.py
def can_book(free_seats: int) -> bool:
    return free_seats > 0

assert can_book(1)
assert not can_book(0)
print("OK")

# python3 booking.py

Try it yourself

Predict each result, then run the two assertions.

Reveal the explanation
  1. can_book(1)

    Returns True. The first assertion passes.

  2. can_book(0)

    Returns False. The second assertion passes.

Both assertions run before OK is printed. Change > to >= and check which assertion fails.

Shared booking contract

Accept a booking when at least one seat is free.

Read free seats
Is the count greater than zero?
Yes
Accept
No
Decline
Free seatsExpected
0false
1true
2true
Runnable reference files
Download shared test cases

Your turn

Run the Python checker with the shared contract. Then change the comparison and rerun.

Common mistake Placing return outside the function body.

Check your work

Capture the passing checks and identify which case fails after the change.