[Tutor] Leap years

Gonçalo Rodrigues op73418@mail.telepac.pt
Sun Jan 12 13:29:01 2003


----- Original Message ----- 
From: "ahimsa" <ahimsa@onetel.net.uk>
To: <tutor@python.org>
Sent: Sunday, January 12, 2003 5:14 PM
Subject: [Tutor] Leap years


> Hello
> 
> Although the problem is relatively straight forward, I am wanting to
> think through the problem-solving process itself.
> 
> I am trying to script a program that will enable a user to input a given
> 4 digit year and have the output state whether or not that year is a
> leap year.
> 

The rule is actually more complex:
i ) A leap year is divisible by 4.
ii) Exception to i) if it is divisible by 100 then it's not a leap year.
iii) Exception to ii) if it is divisible by 400 then it is a leap year.


> The basic idea is that a leap year happens once every four years, so
> therefore the year given should be divisible by 4 with no remainder.
> However, this doesn't always give an accurate result.
> 
> This is the program thus far:
> 
> _______________________________________________
> # Calculating leap years for user input:
> 
> year = input( "What year: " )       # Get user input
> 
> leap = year % 4                     # Leap years are divisible by 4
> 
> if leap == 0:                       # Modulus should be zero
>     print "%s IS a leap year" % year
> else:
>     print "%s is NOT a leap year" % year
> ________________________________________________
> 
> On the surface, this does work and correctly outputs leap years for:
> 1988, 2000, 2400, 1996, etc.
> 
> Where it falls short is on those centuries that are not leap years, such
> as 2100. Those who use Linux can get a calendar to test this: $ cal 02
> 2100 , etc. Clearly, Feb 2100 only has 28 days, not the required 29.
> 

See above for the complete rule.

> So now, I am stuck :(
> 
> Rather than an answer, if someone can help me think through this problem
> so that I can learn the process/method involved, that would be best for
> me.
> 
> Thank you in anticipation.
> 
> Andrew
> 

All the best,
G. Rodrigues