Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

When I input the year "2020", I get a double output of "2020 is a leap year." and I am not sure why

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".

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading