158. Python Program to Keep Asking for a Valid Age
MediumSafe input
Overview
The safe input loop: never crash, ask again until the answer is valid.
Problem Statement
Read an age, and keep asking until it is valid:
- If the input is not a whole number, print
Please type a whole numberand read again. - If it is a whole number but not from 1 to 120, print
Age must be 1 to 120and read again.
When the age is valid, print it, like this for 30:
Your age is 30
Use input() with no prompt text: the tests type the input for you, and a prompt would end up in your output.
Input
One value per line, until a valid age. Example:
abc
0
200
30
Output
The output for that example:
Please type a whole number
Age must be 1 to 120
Age must be 1 to 120
Your age is 30
The tests try 4 different inputs.