Need Help, if can. This is in Python. Implement a class Person that has these me
ID: 3533337 • Letter: N
Question
Need Help, if can. This is in Python.
Implement a class Person that has these methods -
1) __init__ : constructor that initializes a person's name and birth year
2) getAge : returns the age of the person based on current year.
Hint - use the built-in localtime( ) function of the time package ( from time import * )
currentTime = localtime()
year = currentTime[0] # example that gives the current value of year
3) getName : returns the name of the person
**************************** Hints Given:
example test code -
p1 = Person('Joe',2000)
print ( "%s is %d years old" %( p1.getName( ), p1.getAge( ) ) ) # should print -> Joe is 13 years old
p2 = Person('Bob',1995)
print ( "%s is %d years old" %( p2.getName( ), p2.getAge( ) ) ) # should print -> Bob is 18 years old
Explanation / Answer
from time import *
class Person():
def __init__(self,name,byear):
self.name=name
self.byear = byear
def getAge():
currentTime=localtime()
year = currentTime[0]
return year-self.byear
del getName():
return self.name
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.