0

I'm trying to write a simple code which gets the birth date of the user and prints how many days so far they have lived. Although it's not that precise (I didn't add the month day conditions to determine how many days exactly each month includes), there seems to be more serious calculation problem I'm not getting right. It somehow shows way more days than it's supposed to. I wonder why.

Here's my code:

print('Enter your birth date in the following format: \nDD/MM/YYYY')
birth_date = input()

birth_day = int(birth_date.split('/')[0])
birth_month = int(birth_date.split('/')[1])
birth_year = int(birth_date.split('/')[2])

current_year = 2021
current_month = 3
current_day = 8

extra_days = ((current_year - birth_year) // 4) + ((current_year - birth_year) // 400) + 1

this_year_days = 67 #Jan=31, Feb=28, Mar=8th

birth_year_days = birth_day + ((birth_month - 1)*30)

total_days = ((current_year - birth_year)*365) + (this_year_days + birth_year_days) + extra_days

print(total_days)

When I give the same dates to online age calculation websites they return less amounts than what I get shown here. For example, for 26/04/2000 the internet shows 7622 days, but my code returns 7853 days. I know I'm not having the best solution to this problem, but I can't figure out what I have not taken into consideration.

I'd appreciate it in advance if anyone could help me with this.

  • 1
    `from datetime import datetime; print(datetime.now() - datetime(2000, 4, 26))`… – deceze Mar 08 '21 at 09:23
  • Where are you accounting for months in your calculation? – khelwood Mar 08 '21 at 09:23
  • @deceze I'm supposed to do it manually. I can't simply take the one line code. –  Mar 08 '21 at 09:25
  • @khelwood What do you mean exactly? –  Mar 08 '21 at 09:26
  • Months have different lengths. You're not accounting for that. You're just subtracting 1 from `birth_month` and multiplying it by 30. – khelwood Mar 08 '21 at 09:28
  • @khelwood I'm fully aware of that, and even mentioned above that I know the result may not be that precise to the real one, but consider, if that was the only problem, my code will only have a few days difference with the real one; not 2000 days or more! This proves that the problem is something else. –  Mar 08 '21 at 09:30

1 Answers1

0

As deceze suggested, using the datetime module could make this a lot easier, and for me the result was only off by about 400 days.

However continuing with your method, one problem is the months, as mentioned. Another is the birth_year_days. You're measuring days since the year started, not days until the year is finished (if that makes sense). Try this instead:

birth_year_days = (30 - birth_day) + ((12 - birth_month)*30)

So then, you also have to remove 1 year from your total_days calculation:

total_days = ((current_year - birth_year - 1)*365) + (this_year_days + birth_year_days) + extra_days

And this gets your method to within 3 days of the actual answer for me anyway.

For reference, and to use if you want to test your way of doing it, using the datetime module you can do it like this:

from datetime import datetime


actual = datetime.now() - datetime(year=birth_year, month=birth_month, day=birth_day)

print(actual)

(where birth_year etc. are defined the same as in your block of code)

Rolv Apneseth
  • 2,078
  • 2
  • 7
  • 19
  • Sorry, I read in the comment that yours was 2000 days off but your question does say just 200 so never mind the first sentence – Rolv Apneseth Mar 08 '21 at 11:25