Space Python Loading Python…

Clear error messages

Write the codeMedium 2 stages

A planning script asked fuel_needed() about a burn of -5 seconds, after a sign error somewhere upstream, and got back -62.5 kg. Nobody noticed until the fuel budget came out 62.5 kg short.

A function that gets a value it can't work with should say so, loudly and clearly, at the moment it happens.

Make fuel_needed() raise ValueError for a negative burn length, with a message that shows the value, so the calls below it print:

500.0
Refused: burn length can't be negative: -5 s

Write raise ValueError(f"burn length can't be negative: {burn_s} s") inside an if, before the calculation. A burn of 0 seconds is allowed.

Refuse a negative length
A message that helps

Finish stage 1 to unlock this stage.

A burn length read from a file arrives as the text "40", and the function stops with a confusing TypeError from the comparison. Check the type first and raise your own TypeError, naming the type that came in:

500.0
Refused: burn length can't be negative: -5 s
Refused: burn length must be a number, got str

Use if not isinstance(burn_s, (int, float)):, and type(burn_s).__name__ for the type's name. It has to come before burn_s < 0, which is the line that fails for text.

Refuse text
The right error for each problem
You're not logged in, so your progress is kept in this browser only. Log in to save it, and to use hints and solutions.

Hints

script.py
PythonLn 1, Col 1Spaces: 4
Loading the Python environment…

Separate arguments with commas. They're passed on every Run as sys.argv[1:].