Run Your First Function in Python
Scenario
Save the example as booking.py and run python3 booking.py.
# 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.pyTry it yourself
Predict each result, then run the two assertions.
| Case | Result |
|---|---|
| can_book(1) | Select a choice above to see what happens. |
| can_book(0) | Select a choice above to see what happens. |
Reveal the explanation
- can_book(1)
Returns True. The first assertion passes.
- 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.
| Free seats | Expected |
|---|---|
| 0 | false |
| 1 | true |
| 2 | true |
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.
This browser could not save your progress. Changes last until you leave the page.