Using Python -- Implement a class Person that has these methods: 1) __init__ : c
ID: 3763451 • Letter: U
Question
Using 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
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
class Person():
def __init__(self, a, b):
self.name=a
self.age=b
def getName(self):
return self.name
def getAge(self):
return self.age
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
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.