The following shows how the Person class and its methods could be used: Le Pytho
ID: 3838159 • Letter: T
Question
The following shows how the Person class and its methods could be used: Le Python 3.5.0 Shell File Edit Shell Debug Options Window Help Python 3.5 0 (v 3. 5. 0:3 74f 501f4 56 Sep 13 2015 02:1 [MSC v. 190 tel) on Win32 Type II copyright "credits" or "license for more information. C: Users jeand 000 Desktop Csc2 42 asg2.py RESTART pl Person ("Barb Wire 1951) pl Person (Barb Wire, 1951) print (pl) Barb Wire is 65 years old val pl age val 65 pl name Barb Wire pl by ear 1951 p2 Person p2 Person (Jane Doe, 2016) print (p2) Jane Doe is 0 years old val p2 age val p2 name Jane Doe p2.byearExplanation / Answer
main.py
class Person(object):
'a class representing an abstraction of a person'
def __init__(self, name = 'Jane Doe', birthYear = localtime()[0]):
'the constructor'
self.n = name
if birthYear > localtime()[0]:
raise InvalidDate('{} is not a valid birth year'.format(birthYear))
else:
self.birthYear = birthYear
def age(self):
'returns the age of the person, using the local method of the time module'
timeLst = localtime()
return timeLst[0] - self.birthYear
def name(self):
'returns the name of the person'
return self.n
def __repr__(self):
'gives the representation of a Person'
return 'Person({}, {})'.format(self.n, self.birthYear)
def __str__(self):
'returns a formatted version of the Person'
return '{} is {} years old.'.format(self.n, self.age())
# Put the InvalidDate exception class here
class InvalidDate(Exception):
'An exception class for Person'
pass