# Python 2.5 Help



## woody2371 (May 10, 2007)

Hey, i got this code to work out whether or not it is a leap year, i defined it in a module.

def leapyear(year):
ret = 0
if(year % 1000):
ret = 1
elif(year % 400):
ret = 1
elif(year % 4):
ret = 1
if (ret == 1):
isleapyear = "Yes it is a leap year"
elif (ret == 0):
isleapyear = "No it is not a leap year"
return isleapyear

Now when i run it, inputting 4 as the year var makes it say correctly it is a leap year, as does 8,12,16 etc etc.

However, when i input 1000 or something around there, it says it is NOT a leap year. Anyone know why?

Cheers


----------



## Kalim (Nov 24, 2006)

I don't know much of Python, but a friend I visited was using this script for what you're wanting to do:


```
def isLeapYear (year):
    result = year % 4
    if result > 0:
        print "It is NOT a leap year"
    else:
        print "It IS a leap year"

        result = (year * 100) % 4
        if result >0:
            print "It is NOT a leap year"

isLeapYear(400)
isLeapYear(800)
isLeapYear(1600)
isLeapYear(1601)
isLeapYear(2004)
isLeapYear(2012)
isLeapYear(3010)
isLeapYear(3330)
```
I know it had initial problems with calculating figures over a few centuries so she later had to tweak it more. It worked well, but IDK if it was for Python 2.4, earlier or 2.5.1, sorry. :4-dontkno

Maybe it will be of some help.


----------



## Deckard (May 8, 2006)

I believe the leap year formula for the Gregorian calendar (post-1582) is:



http://en.wikipedia.org/wiki/Leap_year#Leap_year_algorithms said:


> if year modulo 400 is 0 then leap
> else if year modulo 100 is 0 then no_leap
> else if year modulo 4 is 0 then leap
> else no_leap


Before 1582, you'd use the Julian calendar.


----------

