'I'm on a project trying to create a leap year generator. Every year works besides the years with the last two digits being 16 or 20
Any time I type the years that end with 16 or 20, example: 2016 or 3020, it just ends. The code I am using is below, any help would be much appreciated. Thank you.
year = int(input("Year??? ")
if year % 4 == 0:
if year % 100 == 0:
if year % 400 == 0:
print("Leap year.")
elif year != 0:
print("Not leap year.")
else:
print("Not leap year.")
Solution 1:[1]
Try something like this:
year = int(input("Year???"))
if (year%400 == 0):
print("Not a leap year")
elif (year%100 == 0): # elif is preferable to nested if in most cases
print("Not a leap year")
elif (year%4 == 0):
print("This is a leap year!")
else:
print("This is not a leap year")
Solution 2:[2]
year = int(input("Year???"))
if not year % 4 and (year % 100 or not year % 400):
print("This is a leap year!")
else:
print("Not a leap year")
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | alien_jedi |
| Solution 2 | Vovin |
