Course curriculum
Python · LESSON 5 OF 5

Build a reusable study planner

Define functions, return values and combine the earlier lessons into a small planning project.

About 12 minutes · Beginner

Give a task a name

A function groups a task behind a name. Parameters describe the inputs; return sends a result back to the caller.

Printing displays a message, while returning lets another part of your program use a value. A function that reaches its end without return returns None.

Python
def remaining_pages(total, completed):
    return max(0, total - completed)

print(remaining_pages(80, 25))
Expected output
55

Validate a plan

This planner calculates whole study days. Integer ceiling division (remaining + daily - 1) // daily rounds up when a partial final day is needed.

Reject a daily target of zero before dividing. The function returns a number; the caller decides how to present it.

Python
def study_days(total, completed, daily):
    if daily <= 0:
        raise ValueError("Daily target must be positive")
    remaining = max(0, total - completed)
    return (remaining + daily - 1) // daily

print(study_days(80, 25, 10))
Expected output
6

Check the project

Try a plan that is already complete, a plan with exactly one day left and a plan with a partial last day. Each case checks a different part of the calculation.

Keep inputs as whole non-negative page counts. A larger application would validate user input before calling this function and handle ValueError with a clear message.

Try it yourself

Call study_days for 100 total pages, 40 completed pages and a daily target of 15. Then check an already completed plan.

Show a possible solution
Python
print(study_days(100, 40, 15))  # 4
print(study_days(100, 100, 15))  # 0

Keep exploring: official Python documentation.

0 of 5 completed on this device