[Python]The version of the Person class developed for the second assignment allo
ID: 3838186 • Letter: #
Question
[Python]The version of the Person class developed for the second assignment allows the creation of people who have not yet been born since it does not have any error checking on the birth year used to create the object. For this question you will create a new exception class InvalidDate that will be raised in the constructor for the person class when a Person is created whose birth year is after the current year. Carefully examine the sample output below to ensure that you are providing the required information when raising the exception. Also note that this problem does not involve the use of any try-except blocks. The following shows the way that the new Person class and its methods would behave when given invalid parameters, with some parts of the output redacted to not give away the answer to the question:
>>> p1 = Person('Amber Settle', 1968)
>>> p1
Person(Amber Settle, 1968)
>>> print(p1)
Amber Settle is 47 years old.
>>> p2 = Person("Ohto Kurhila", 2017)
Traceback (most recent call last):
File "<pyshell#49>", line 1, in <module>
p2 = Person("Ohto Kurhila", 2017)
File "C:UserssettleDocumentsCoursesCsc 242AssignmentsHw 4csc242hw4Sol.py" in __init__
[omitted to not give away the answer to the question]
InvalidDate: 2017 is not a valid birth year.
>>> p2
Traceback (most recent call last):
File "<pyshell#50>", line 1, in <module>
p2
NameError: name 'p2' is not defined
>>> p2 = Person('Aukko Vihavainen', 2016)
Traceback (most recent call last):
File "<pyshell#51>", line 1, in <module>
p2 = Person('Aukko Vihavainen', 2016)
File "C:UserssettleDocumentsCoursesCsc 242AssignmentsHw 4csc242hw4Sol.py" in __init__
[omitted to not give away the answer to the question]
InvalidDate: 2016 is not a valid birth year.
>>> p2
Traceback (most recent call last):
File "<pyshell#52>", line 1, in <module>
p2
NameError: name 'p2' is not defined
>>>
Explanation / Answer
Person class with InvalidDate() exception. Your exception class should be inside construcor
from time import localtime
class Person(object):
def __init__(self, name, birthyear):
# Your exception class shpuld be up here inside constructor . it will not work at the place you specified in your question
class InvalidDate(Exception):
pass # Because you just need to give a TRACEBACK and raise an exception, Empty exception class would to the trick here
self.n=name
if birthyear > localtime()[0]:
raise InvalidDate('{0} is not a valid birthyear '.format(birthyear)) # the best way is to write the error message in argument
else:
self.birthyear = birthyear
def age(self):
return localtime()[0] - self.birthyear
def name(self):
return self.n
def __repr__(self):
return ('Person({0} {1})'.format(self.n, self.birthyear))
def __str__(self):
return ('{0} is {1} years old'.format(self.n, self.age()))
# For testing delete further code after testing
na = input('name')
b = int(input('b'))
p1 = Person(na,b )
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.