Curriculum

Curriculum

Return a Value Instead of Printing It

Scenario

Compare two versions of canBook(freeSeats).

Selected choicePick a choice below
Result—

Try it yourself

Select each version to see which one an automated assertion can verify.

Reveal the explanation
  1. function canBook(freeSeats) { print(freeSeats > 0) }

    Hard to test. The function prints to the console and returns nothing (None/undefined).

  2. function canBook(freeSeats) { return freeSeats > 0 }

    Testable and reusable. assert(canBook(1) === true) checks the exact returned boolean.

  3. Caller formats returned boolean into "Accepted" / "Declined" at the UI edge

    Clean separation: one rule function works for CLI, web UI, and tests alike.

Keep decision functions focused on returning values. Let the outer UI layer decide how to format or print that returned result.

Review first

Your turn

Refactor "def did_pass(score): print(score >= 60)" so a test can assert did_pass(60) is True.

Common mistake Leaving print() inside did_pass and forgetting return.

Check your work

def did_pass(score): return score >= 60.

Quick check

Why does "assert can_book(1) is True" fail if can_book only calls print(True)?
Show the answer and reasoning

Correct answer Because print writes to the screen and the function returns None instead of True

Without a return statement, the caller receives None rather than the boolean result.