Space Python Loading Python…

Safe number input

Write the codeEasy 2 stages

The flight engineer types the burn length for the next manoeuvre. One morning the keyboard was set to a Polish layout, 12,5 went in instead of 12.5, and the whole console program stopped with a ValueError.

Make it ask again instead, for as long as it takes to get a usable number.

Keep asking until the input converts to a number. For anything that doesn't, print Not a number: and what was typed, then ask again. With the answers twelve, 12,5 and 12.5, the program prints:

Not a number: twelve
Not a number: 12,5
Burn: 12.5 s

Use while True:, with float(text) inside try, break when it works, and except ValueError: for the message. The check doesn't show the question itself, only what the program prints.

A good number at once
Ask again after a typo

Finish stage 1 to unlock this stage.

A burn of 0 or -5 seconds converts fine, but makes no sense. Turn those away too, with Must be more than 0: and what was typed. With -5, abc, 0 and 12.5, it prints:

Must be more than 0: -5
Not a number: abc
Must be more than 0: 0
Burn: 12.5 s

Keep the try around the conversion only. Inside except, use continue to go straight to the next question; after the try, check the number.

Turn away bad lengths
Typos still handled
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:].