Reputation: 57
so I have this exercise to do where it is asking me to get the age of someone, in terms of days, based off an input of their birthdate and current date. For instance, (2012,1,1,2012,2,28) should return 58 (Also I am not allowed to use any built in time functions). Here is my code so far:
def leapyear(year):
return year % 4 == 0 and (year % 100 != 0 or year % 400 == 0)
def daysBetweenDates(year1, month1, day1, year2, month2, day2):
age = 0
months31 = [1,3,5,7,8,10,12]
months30 = [4,6,9,11]
while year1 != year2 and month1 != month2 and day1 != day2:
age += day2
day2 = 0
if day2 == 0:
month2-=1
if month2 in months31:
day2 += 31
month2 -= 1
if month2 in months30:
day2 += 30
month2 -= 1
if month2 == 2:
if leapyear(year2):
day2 += 29
month2 -= 1
else:
day2 += 28
month2 -= 1
else:
year2 -= 1
month2 = 12
return age
Anyways my problem is that whenever I try this function it keeps returning with 0, any help to fix this is appreciated.
EDIT: Hey guys, so based off of your feedback I understood why my code was taking forever to run so I changed it up so it looks like this now:
def leapyear(year):
return year % 4 == 0 and (year % 100 != 0 or year % 400 == 0)
def daysBetweenDates(year1, month1, day1, year2, month2, day2):
# Your code here.
age = 0
months31 = [1,3,5,7,8,10,12]
months30 = [4,6,9,11]
if day1 != day2:
age = abs(day2-day1)
day2 = day1
if month1 > month2:
while month1 != month2:
if month2 in months31:
age += 31
month2 += 1
if month2 in months30:
age += 30
month2 += 1
if month2 == 2:
if leapyear(year2):
age += 29
month2 += 1
else:
age += 28
month2 += 1
else:
while month1 != month2:
if month2 in months31:
age += 31
month2 -= 1
if month2 in months30:
age += 30
month2 -= 1
if month2 == 2:
if leapyear(year2):
age += 29
month2 -= 1
else:
age += 28
month2 -= 1
while year1 != year2:
if leapyear(year2):
age += 365
year2 -= 1
else:
age += 365
year2 -= 1
return age
But the only problem is that for certain examples it's working while for others I am getting close to the answer but not the exact answer. Example, for print(daysBetweenDates(2012, 1, 1, 2012, 2, 28))
I should get 58 when I am getting 56.
Upvotes: 0
Views: 140
Reputation: 4606
Off by none! day but this is a better approach you can use to calculate this process. Had a few errors on first calculation, now it runs great! Enjoy :)
def leap_year(y):
if y % 4 == 0 and (y % 100 != 0 or year % 400 == 0):
return True
return False
def year_to_date(yeah, month, day):
days = 0
days += day
for i in range(0, month - 1):
if i == 1:
if leap_year(year):
days += 28
else:
days += 29
else:
days += days_in_month[i]
return days
def birth_to_new_year(year, month, day):
days = 0
days += days_in_month[month - 1] - day
for i in range(month, 12):
if i == 1:
if leap_year(year):
days += 28
else:
days += 29
else:
days += days_in_month[i]
return days
days_in_month = [31, 0, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
day = 18
month = 9
year = 2018
birth_day = 20
birth_month = 11
birth_year = 1988
days = 0
for i in range(birth_year + 1, year):
if leap_year(i):
days += 366
else:
days += 365
days += year_to_date(year, month, day)
days += birth_to_new_year(birth_year, birth_month, birth_day)
print(days)
Upvotes: 1