<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 TRANSITIONAL//EN">
<HTML>
<HEAD>
  <META HTTP-EQUIV="Content-Type" CONTENT="text/html; CHARSET=UTF-8">
  <META NAME="GENERATOR" CONTENT="GtkHTML/3.14.2">
</HEAD>
<BODY>
Thanks Alan,<BR>
<BR>
I managed to get totally confused by the different definitions of calculating a leap year. The book (Core Python) <BR>
described the exercise thus:<BR>
<BR>
&quot;Modulus.&nbsp; Determine whether a given year is a leap year, using the following formula: a leap year is one that is divisible by four, but not <BR>
by one hundred, unless it is also divisible by four hundred.&quot;<BR>
<BR>
That sounded to me like:&nbsp; y % 4 ==0 and (y % 100==1 or y % 400==0)<BR>
Then, after reading it ten more time I became uncertain and decided to find another definition and compare them.<BR>
I found U.S. Naval Observatory:<BR>
<BR>
&quot;According to the Gregorian calendar, which is the civil calendar in use today, years evenly divisible by 4 are leap years, with the exception of centurial years that are not evenly divisible by 400.&quot;<BR>
 <BR>
Comparing the two, confused me a bit more, and I ended up ignoring the first definition, saying to myself, all 4th years are leap years, unless they are y % 100 ==0 AND y % 400==1. That sounded clearer....but, evidently, not correct. Ha Ha Ha&nbsp; <BR>
<BR>
I liked the way you directly formed it. It was more like my old IF-THEN statements.....to the point and we're out of here.<BR>
<BR>
def isLeapYear(y):<BR>
&nbsp;&nbsp;&nbsp; if y % 4 == 0: return True<BR>
&nbsp;&nbsp;&nbsp; if (y % 4 == 0) and not (y %100 == 0): return True<BR>
&nbsp;&nbsp;&nbsp; else: return False<BR>
<BR>
I am checking out your python tutor web site at <A HREF="http://www.freenetpages.co.uk">http://www.freenetpages.co.uk</A><BR>
It looks like it surpasses by far other's I have checked out. Great job!<BR>
<BR>
Thanks for the correction and the methodology!<BR>
<BR>
Terry
<PRE>
&gt; def isLeapYear(y):
&gt;    if y % 4 == 0:
&gt;        if y % 100 == 0 and y % 400 == 1:
&gt;            answer = False
&gt;            return answer
&gt;        answer = True
&gt;        return answer
&gt;    answer = False
&gt;    return answer

Not quite. y%400 == 1 will only be true for years
like 2001, 1601 etc!
You need to invert the test so y % 400 != 0.  (or use not)
But there is a more direct way:

Lets recap:

1) A year that is divisible by 4 is a leap year. (Y % 4) == 0
2) but: a year that is divisible by 100 is not a leap year. (Y % 100) 
!= 0 3)
3) however a year that is divisible by 400 is a leap year. (Y % 400) 
== 0

So a year that is divisible by 400 is *always* a leap year:

   if y % 400 == 0: return True

Now we need a combined test for the other 2 cases.
If its divisible by 4 and not by 100 its a leap year:

if (y % 4 == 0) and not (y %100 == 0): return True

So we can combine the definitions to give:

def isLeapYear(y):
   if y % 400 == 0: return True
   if (y % 4 == 0) and not (y %100 == 0): return True
   else: return False

Which is shorter and clearer IMHO.
(You could combine it into one line but I personally
think that would make the rules less clear.)

This is one of the cases where using multiple returns
makes the code clearer in my view.

&gt; print &quot;This program finds all leap years between any two
&gt; dates.&quot;;print;print
&gt;
&gt; start = end = None
&gt; while start == None:
&gt;    try:
&gt;        start = int(raw_input(&quot;Enter yyyy for beginning year : &quot;))
&gt;        end = int(raw_input(&quot;Enter yyyy for ending year : &quot;))
&gt;
&gt;    except ValueError:
&gt;        print;print &quot;YEAR must be a integer number -- TRY AGAIN!&quot;
&gt;        start = end = None
&gt;
&gt; if 1 &lt;= start &lt; end:
&gt;    print; print
&gt;    for y in range(start, end + 1):
&gt;        answer = isLeapYear(y)
&gt;        if answer == True:
&gt;            print y, &quot;--leap year!&quot;

Also the point of a predicate function is that it returns
a boolean value so you don;t need any intermediate
variables. You can just use it in the condition, like

if isLeapYear(y):
   print y, '--leap year'

By using a predicate style name (isXXXXX) the code
becomes clearer than when you introduce an intermediate
variable where the reader then has to backtrack to see
what the test value is based on.

&gt; 1900 --leap year!

This is wrong, 1900 is divisible by 100 but not by 400...

&gt; 1904 --leap year!
&gt; 1908 --leap year!
&gt; ...
&gt; 2000 --leap year!

Whereas 2000 is correct because it is divisible by 400.

HTH,

</PRE>
</BODY>
</HTML>