Practice · Python

Practice Raising & Validating Input

Validate arguments and raise appropriate exceptions (ValueError/TypeError) to enforce a function's contract.

Raising & Validating Inputwarming python…

Average Song Tempo with Validation

Write a function average_tempo(tempos) that receives a list of song tempos measured in beats per minute (BPM). The function should return the arithmetic average as a float.

  • If the list is empty, raise a ValueError with an appropriate message.
  • If any tempo is less than 20 or greater than 300, raise a ValueError indicating the offending value.

Examples:

average_tempo([60, 120, 180])   # returns 120.0
average_tempo([20, 300])        # returns 160.0
Press Run to see output, or Check to grade against the tests.

Want a whole course built around raising & validating input?

Start my path — free

Adaptive practice, streaks, projects with real code review. Free tier gates AI help, never content.