Home > OS >  When I input the year "2020", I get a double output of "2020 is a leap year." an
When I input the year "2020", I get a double output of "2020 is a leap year." an

Time:03-27

year = int(input("Which year do you want to check? "))
if year % 4 == 0:
 print(year, "is a leap year.")
 if year % 100 == 0:
  print(year, "is not a leap year.")
  if year % 400 == 0:
   print(year, "is a leap year.")
  else:
   print(year, "is not a leap year.")
 else:
  print(year, "is a leap year.")
else:
 print(year, "is not a leap year.")

I have tried changing the nested "if" conditionals into "elif" ones where it seemed appropriate, but it ends up with the same results. While the logic is correct in whether or not the input year is a valid leap year or not on other years without a double print result, I am not sure why it produces a double print result on the year "2020".

CodePudding user response:

here is one way to calculate leap year:

def isleap(year):
    """Return True for leap years, False for non-leap years."""
    return year % 4 == 0 and (year % 100 != 0 or year % 400 == 0)
  • Related