Raising errors — making your code fail loudly on purpose — step 8 of 9
Write a function validate_age(age) that:
- Returns
ageunchanged whenageis an integer between0and120(inclusive). - Raises
ValueErrorwith the messagef"age out of range: {age}"otherwise.
Then call it three times inside try/except ValueError as err: print(err)
blocks so the output below prints. Use print(validate_age(...)) for
the happy-path call.
Expected output:
30
age out of range: -5
age out of range: 200
⌘↵ runs the editor.read, then continue.
Write a function validate_age(age) that:
- Returns
ageunchanged whenageis an integer between0and120(inclusive). - Raises
ValueErrorwith the messagef"age out of range: {age}"otherwise.
Then call it three times inside try/except ValueError as err: print(err)
blocks so the output below prints. Use print(validate_age(...)) for
the happy-path call.
Expected output:
30
age out of range: -5
age out of range: 200
this step needs the editor
on desktop today; in the app (coming soon). save your spot and we'll bring you back here when you're ready.