[Tutor] How to test for a remainder from division

Matt Smith matt at mattanddawn.orangehome.co.uk
Tue May 15 08:30:40 CEST 2007


Hi,

Thanks for all the help, I guessed that there would be a module out
there providing a function to do this but wanted to go through the
process as a learning exercise. The modulus operator was just what I was
looking for, I had been trying to check for a difference between the
division and the floor division - a bit of a long winded way of doing
things. Here 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
    else:
        return False

# Main program logic
year = raw_input("What year? ")
year = int(year)
if is_leap_year(year):
    print year, "is a leap year."
else:
    print year, "is not a leap year"

Thanks,

Matt




More information about the Tutor mailing list