Can someone help me with the correct code I should use please? I still haven't figured it out.
I've posted the code below and the error outcome I'm receiving below. Some ages are being reported incorrectly. Can someone help me with the correct code please? Thank you for your time and any assistance provided.
Someone below helped me figure out their birthdates.
(Sarah = 2000/08/01, Eric = 2009/08/02, Carter = 2009/07/28, Georgia = 2005/09/01)
This is the code:
from datetime import date
class Person:
def __init__(self, name, birthdate):
self.name = name
self.birthdate = birthdate
def get_age(self):
birthdate = self.birthdate
today = date.today()
if today.month >= birthdate.month and today.day >= birthdate.day:
self.age = (today.year - birthdate.year)
else:
self.age = (today.year - birthdate.year) - 1
return self.age
This is the outcome (some of the age tests are failing):
#TEST 1#
sara.get_name() returned Sara
inputs:
outputs:
----------
#TEST 2#
sara.get_height() returned 160
inputs:
outputs:
----------
#TEST 3#
sara.get_age() returned 20
inputs:
outputs:
----------
#TEST 4#
sara.get_description() returned Sara is 160 cm high and is 20 years old.
inputs:
outputs:
----------
#TEST 5#
eric.get_age() returned 10
inputs:
outputs:
----------
#TEST 6#
** ERROR **carter.get_age() returned 10
* EXPECTED * 11
inputs:
outputs:
----------
#TEST 7#
georgia.get_age() returned 14
inputs:
outputs:
I think you have a logic error in get_age()
:
def get_age(self):
birthdate = self.birthdate
today = date.today()
if today.month >= birthdate.month and today.day >= birthdate.day:
self.age = today.year - birthdate.year
else:
# haven't reached birthday yet
self.age = (today.year - birthdate.year) - 1
return self.age
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.