[Tutor] Leap years

Don Arnold Don Arnold" <darnold02@sprynet.com
Sun Jan 12 13:24:01 2003


----- Original Message -----
From: "ahimsa" <ahimsa@onetel.net.uk>
To: <tutor@python.org>
Sent: Sunday, January 12, 2003 11:14 AM
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.

That's a good place to start.

>
> 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.

That is because your definition of a leap year is incorrect: a year is a
leap year if it is divisible by 4 and not by 100, or is divisible by 400.
Using the correct definition will get rid of those 'false positives'.

<snip>

Once you get that working, you might want to see about turning this test
into a function. In pseudocode:

def isLeapYear(year):
    if year passes test for a leap year:
        return 1
    else:
        return 0


This will return 1 (which is considered 'true') if the input year passes the
test, or 0 (which is 'false') if it doesn't. That allows you to structure
your print statements (or other conditionals) like this:

if isLeapYear(year):
    print '%s is a leap year' % year
else:
    print '%s is not a leap year' % year


>
> 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.

That's good to hear. It lets us know that we're not doing one of your
homework assignments for you.   ; )

>
> Thank you in anticipation.
>
> Andrew
> --
> ahimsa <ahimsa@onetel.net.uk>

HTH,

Don