error handling — when ai's code crashes mid-flight8 / 9
raising errors — making your code fail loudly on purpose
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.
Booting Python…
Output
[promptdojo:~]$ _