156. Python Program to Check an Age with raise
Easyraise
Overview
Raise the error in the function, catch it where you call the function.
Problem Statement
Write a function check_age(age) that raises a ValueError with the message Age must be 0 to 120 when the age is below 0 or above 120, and returns the age otherwise.
Read a whole number, pass it to check_age(), and print the saved age, like this for 25:
Age saved: 25
If any ValueError happens, print Error: and its message. For 150:
Error: Age must be 0 to 120
A word like abc is caught by the same except, so it prints Python's own message:
Error: invalid literal for int() with base 10: 'abc'
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. Example:
25
Output
The output for that example:
Age saved: 25
The tests try 5 different inputs.