Skip to content
Aabha AI Academy
Skip to reading

Python · Module 4

Answers and explanations · Functions and modules

Compare these references after your own attempt. You can read them at any time; they do not record a live model or vendor run.

Module 4

A returned result can feed the next calculation. The two sessions total 60 minutes, which converts to 1.0 hour. Empty summaries have zero count and total, while negative duration violates the conversion contract.

Download the complete answer

# Reference answer. Attempt the exercise first.
def minutes_to_hours(minutes):
    if minutes < 0:
        raise ValueError("minutes must be non-negative")
    return minutes / 60

def summarize_minutes(values):
    return {"count": len(values), "total": sum(values)}

values = [15, 45]
summary = summarize_minutes(values)
print(summary)
print(minutes_to_hours(summary["total"]))
assert summarize_minutes([]) == {"count": 0, "total": 0}
assert minutes_to_hours(0) == 0
try:
    minutes_to_hours(-1)
except ValueError:
    print("Negative-input check passed")
else:
    raise AssertionError("Expected ValueError")

Exit-check guide: A parameter is a name in the definition; an argument is supplied by a call. Printing presents information, while return provides a result to the caller. Explicit parameters remove hidden global dependencies.

Return to this module