Space Python Loading Python…

Catch what you expect

Fix the codeMedium 3 stages

The fuel monitor reports every single reading as bad, including the ones that look perfectly fine. The sensors were checked twice and they are working. The code has a try and an except, so it can't crash, but the bug is hiding somewhere inside it.

Predict what the program prints, find out what the except is catching, and make it catch only what it should.

Don't run the code yet. Read it and decide what it will print on its last line. Write your guess in prediction, at the top, between the quotes. Then run it and compare.

Any guess passes this stage: the point is to think before you run. After you check, you'll see your prediction next to what Python printed.

Three of the four readings look like numbers. How many end up in good?

Predict, then run
Leave the program as it is

Finish stage 1 to unlock this stage.

Investigate. Change except: to except Exception as error: and print type(error).__name__ in it, to see what is really being caught. Store its name in hidden_error, and the number of readings that are really bad in bad_count.

Modify. Catch only ValueError. The hidden error then stops the program with a traceback: fix it. It must print:

Bad reading: n/a
Good readings: 3
Find what was hidden
Catch only ValueError

Finish stage 2 to unlock this stage.

Write parse_pressure(text), which returns the number, or None when the text is not a number. Catch only ValueError, so a bug such as parse_pressure(None) still raises its TypeError. The loop prints:

101.2
None
100.8
Numbers and None
Only ValueError caught
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:].