In this exercise, you'll raise a manual exception when a condition is not met in a particular function. In particular, we'll be converting birth year to age.
Specifications One a new cell in your notebook, type the following function
import datetime
class InvalidAgeError(Exception):
pass
def get_age(birthyear):
age = datetime.datetime.now().year - birthyear
return age
Add a check that tests whether or not the person has a valid (0 or greater) If the age is invalid, raise an InvalidAgeError Expected Output
>>> get_age(2099)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
__main__.InvalidAgeError
My code is as following, but it shows error on raise line, how can i get expected output?
import datetime
class InvalidAgeError(Exception):
pass
def get_age(birthyear):
age = datetime.datetime.now().year - birthyear
if age >=0:
return age
else:
raise InvalidAgeError
get_age (2099)
raiseline. I think the example is just wrong about that.