──────────────────────────────────────

27. The Scoreboard Workshop · Separate Input from Calculation

Write a function that can be tested with values supplied by any caller.

Lesson files · lessons/functions/separate-roles

Before you start
- Pass a number to a function and use its returned value.
- Read a whole number and put a result in a label.

By the end
- Use one function for the calculation and put input and display in the calling code.
- Test the calculation with several supplied counts, including zero.

Where you can use it
Put a trip time calculation in a function. Call it with example stop counts while you test the rule, then use it with a traveller’s input.

Give travel_minutes the numbers it needs as arguments and return the result. A call such as travel_minutes(3, 5) gives 15 without reading input.

Put input and the f-string in the code that calls the function. This gives you one place to change the calculation and a separate place to change how the result looks.

Predict
Which part could run in a test without asking for input?
- travel_minutes
- int(input())

Worked example (example.py)

def travel_minutes(stops, minutes_per_stop):
    return stops * minutes_per_stop

stops = int(input())
print(f"Minutes: {travel_minutes(stops, 5)}")


Example input
3

Example result
Minutes: 15

Write trip_minutes(stops) with four minutes per stop and a fixed two-minute setup. Read stops and print Minutes: followed by the result.

Starter program (starter.py)

def travel_minutes(stops, minutes_per_stop):
    return stops * minutes_per_stop

stops = int(input())
print(f"Minutes: {travel_minutes(stops, 5)}")


Starter input
3

Hint
Return stops * 4 + 2. Put the f-string in the caller so the function produces a number.

Case 1. Try the sample
Input
3
Expected output
Minutes: 14

Case 2. Setup with zero stops
Input
0
Expected output
Minutes: 2

Stretch
Call the function with two different values in one program to compare the trips.

Reflect
Where would you change the output wording without touching the calculation?

Check your prediction
travel_minutes The calculation function receives everything it needs through arguments.
