72.7k views
1 vote
Write a function def isLeapYear (year) that tests whether a year is a leap year, that is, a year with 366 days. Note that a year is a leap year if it is divisible by 4 but not by 100 , or if it is divisible by 400 . - No print statement in isLeapYear function. It returns True or False - printing must be in the main function - Following are sample runs: enter a year number: 1600 1600 is a leap year enter a year number: 2020 2020 is a leap year enter a year number: 1000 1000 is not a leap year

1 Answer

3 votes

Final answer:

The isLeapYear function checks if a year is a leap year by determining if it is divisible by 4 and not by 100, or divisible by 400. In the provided code, the main function prompts for a year and uses isLeapYear to print if it's a leap year.

Step-by-step explanation:

The function isLeapYear(year) determines if a given year is a leap year. A leap year has 366 days instead of the usual 365, providing a necessary correction to our calendar to synchronize it with the astronomical year. The rules for determining a leap year are specific: a year must be divisible by 4, but not divisible by 100 unless it is also divisible by 400. This means that a year like 2000, which is divisible by 400, is a leap year, but 1900 is not because it is divisible by 100 but not by 400. These rules correct for the overestimation of the solar year (365.25 days) by the Julian calendar. In the Gregorian calendar, the correction of removing three leap days every 400 years gives us a much more accurate average calendar year length.

To implement the isLeapYear function:

def isLeapYear(year):
if (year % 400 == 0) or (year % 4 == 0 and year % 100 != 0):
return True
else:
return False

In the main function, the user is prompted to input a year, upon which the program will use the isLeapYear function to output whether the year is a leap year:

def main():
year = int(input("Enter a year number: "))
if isLeapYear(year):
print(f"{year} is a leap year")
else:
print(f"{year} is not a leap year")

if __name__ == "__main__":
main()
User Marek Dorda
by
8.3k points