0

I have made an age calculator in python that, after you answer a series of questions, gives you your age in years, months and days. I am trying to incorporate leap years in it using an if statement that adds an extra day onto your age for every leap year experience, but I think there could be a shorter way. Any ideas?

Here is my code:

currentDay = int(input('What day of the month is it?'))
currentMonth = int(input('What month is it?'))
currentYear = int(input('What year is it?'))
birthDay = int(input('What day of the month were you born on?'))
birthMonth = int(input('What month were you born?'))
birthYear = int(input('Which year were you born in?'))
ageDays = currentDay - birthDay
ageMonths = currentMonth - birthMonth
ageYears = currentYear - birthYear
daysToAdd = 0

if currentMonth == 1 or currentMonth == 3 or currentMonth == 5 or 
currentMonth == 7:
    daysToAdd = 31

elif currentMonth == 2:
    daysToAdd = 28

elif currentMonth == 8 or currentMonth == 10 or currentMonth == 12:
    daysToAdd = 31

else:
    daysToAdd = 30

if birthDay > currentDay:
    ageMonths = ageMonths + 1
    ageDays = ageDays + daysToAdd

if birthMonth > currentMonth:
    ageMonths = ageMonths + 12

if birthYear < 2016:
    ageDays = ageDays + 1
    if birthYear < 2012:
        ageDays = ageDays + 1
        if birthYear < 2008:
            ageDays = ageDays + 1
            if birthYear < 2004:
                ageDays = ageDays + 1
                if birthYear < 2000:
                    ageDays = ageDays + 1
                    if birthYear < 1996:
                        ageDays = ageDays + 1

print('You are: ', ageYears, ' years, ', ageMonths, ' months, ', ageDays, ' 
days.')
  • 3
    Take a look at [`datetime`](https://docs.python.org/3/library/datetime.html) and [`calendar`](https://docs.python.org/3.7/library/calendar.html). – 0x5453 Jan 29 '19 at 20:17
  • 1
    A much shorter way, using the `datetime` module – roganjosh Jan 29 '19 at 20:17
  • You could use a loop & a list of the years in question. – Scott Hunter Jan 29 '19 at 20:19
  • Python's modules are cool and all, but maybe it would be cooler to explain the user how to do it without them and then talking to him about their usage – Shinra tensei Jan 29 '19 at 20:20
  • If you want to avoid the built-in stuff, a better way to write the leap-year calculations would be to use an algorithm. Your approach above won't incorporate any leap years from 1992 or earlier. Here are a few pages to get you headed in the right direction. https://www.cse.unsw.edu.au/~cs1511/17s2/week02/09_leapYear/ and https://stackoverflow.com/questions/725098/leap-year-calculation – Doug Deden Jan 29 '19 at 20:23

3 Answers3

1

Leap years are defined as being divisible:

  • by 4 but not 100
  • by 400

Using list comprehension and range you can get number of leap years between given years following way:

start_year = 1998 #inclusive
end_year = 2019 #exclusive
leaps =  [i for i in list(range(start_year,end_year)) if (i%4==0 and i%100!=0) or i%400==0]
print(leaps)

output:

[2000, 2004, 2008, 2012, 2016]
Daweo
  • 31,313
  • 3
  • 12
  • 25
  • Your answer does answer my question, but how can I use it to change the output? –  Jan 29 '19 at 20:40
  • In my example `leaps` is list of years, you can get number of years as `nleaps = len(leaps)` and add that value to your `ageDays`. – Daweo Jan 30 '19 at 06:58
  • Thanks for your answer - it has helped me so much. –  Jan 30 '19 at 17:32
0

Without using the said modules, assuming you are doing it for excercise sake, use integer division:

ageDays = ageDays + (2019-birthYear) // 4

Example:

(2019 - 2016) // 4
>> 0
(2019 - 2015) // 4
>> 1
Tarifazo
  • 4,118
  • 1
  • 9
  • 22
  • This is wrong for a number of reasons. 1. It doesn't account for years divisible by 100 (except divisible by 400) not being leap years. 2. It doesn't account for *when* in the year someone was born (or when in the current year it is if this year is a leap year). Someone born in March of a leap year should not have an extra day added, but someone born in January should. – jamesdlin Jan 29 '19 at 20:30
  • You are right, that is why I assumed an exercise (otherwise why would you not use `datetime`??): all I gave was the step to simplify OPs sequence of `if`s – Tarifazo Jan 29 '19 at 20:40
0

Try this code:

# import date to get current year
from datetime import date
# Get current year
current_year = date.today().year
    # your code here...
    # ...
    # loop from birthYear to current year
    for y in range(birthYear, current_year+1, 1):
        # check if leap year
        if y in range(0, date.today().year, 4):
            ageDays = ageDays + 1

Hope it helps!

Carlos Bettin
  • 120
  • 2
  • 11