Practice · Python

Practice Error Handling: try / except

Catch and handle exceptions with try/except/else/finally, targeting specific exception types.

Error Handling: try / exceptwarming python…

Calculate Total Water for Garden Plants

Write a function total_water(water_amounts) that receives an iterable of numbers representing how many litres of water each plant needs. All amounts must be non‑negative. The function should return the total amount of water as a float.

  • If water_amounts is not iterable, raise TypeError.
  • If any element is not an int or float, raise TypeError.
  • If any element is negative, raise ValueError.
  • An empty iterable should yield 0.0.
>>> total_water([1, 2.5, 3])
6.5
>>> total_water([])
0.0
Press Run to see output, or Check to grade against the tests.

Want a whole course built around error handling: try / except?

Start my path — free

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