187k views
2 votes
My code get an input of 1900 and it should output not a leap year but it fails first line of code. It should output not a Leap any number with 1500 not divisble by 4 with/out a remainder should output (not a leap year. )

input_year = int(input())

if input_year % 4 == 0: #fails on this step, 1900 should be false
print(input_year, "- is Leap Year")

elif input_year % 100 == 0:
print(input_year, "- leap year")

elif input_year % 400 ==0:
print(input_year, "is a leap year")

else:
print(input_year, "- not a leap year")

User Xaviel
by
4.6k points

1 Answer

4 votes

Answer:

Step-by-step explanation:

The code does not fail on the first step since 1900 divided by 4 is actually 475 and has no remainder, meaning that it should return True. The code won't work because the if statements need to be nested in a different format. The correct algorithm would be the following, which can also be seen in the picture attached below that if we input 1900 it would output is not a leap year as it fails on the division by 400 which gives a remainder of 0.75

input_year = int(input())

if input_year % 4 == 0:

if input_year % 100 == 0:

if input_year % 400 == 0:

print(input_year, "is a leap year")

else:

print(input_year, "- not a leap year")

else:

print(input_year, "is a leap year")

else:

print(input_year, "- not a leap year")

My code get an input of 1900 and it should output not a leap year but it fails first-example-1
User Mederic
by
4.6k points