# isLeapYear-7.11.py # Determine whether a year greater than 0 is a leap year # Neal Nelson 2007.11.07 # This program illustrates program organization using functions, # exceptions, and conditional execution of main (so the module can # also be used as an import). def isLeapYear(year): # A leap year is any year divisible by 4 except centuries not # divisible by 400 return (year % 4 == 0) and not ((year % 100 == 0 and year % 400 != 0)) def main(): print "This program determines whether a calendar year is a leap year" try: year = input("Enter a year greater than 0 (eg: 1976): ") if year <= 0: # force an exception until we know how to properly define # our own exceptions and raise them. This is a kludge for now, # but better than cluttering up our program. 1/0 if isLeapYear(year): print "Yes, %d is a leap year" % year else: print "No, %d is not a leap year" % year except: print "An input year must be an integer greater than 0." # Execute main only if this file is invoked directly from Python if __name__ == "__main__": main()