[Tutor] How to test for a remainder from division
kent37 at tds.net
Tue May 15 12:26:53 CEST 2007
Matt Smith wrote:
> ere is the final code I have come up with, any comments?
> # A program to determine whether a year is a leap year or not
> def is_leap_year(year):
> # Function that accepts a year as input and returns true if it is a leap
> year, false if it is not
> if year % 4 == 0 and (year % 100 != 0 or year % 400 == 0):
> return True
> return False
There is no need for the if statement, you can return the result directly:
return year % 4 == 0 and (year % 100 != 0 or year % 400 == 0)
> # Main program logic
> year = raw_input("What year? ")
> year = int(year)
> if is_leap_year(year):
> print year, "is a leap year."
> print year, "is not a leap year"
> Tutor maillist - Tutor at python.org
More information about the Tutor