This code is for a leap year calculator/identifier assignment I had. While I passed it, I was wondering why it printed "Leap year" twice depending on some years. Sometimes it did, sometimes it didn't depending on the host I was using, eg. Thonny, Coding Rooms, replit.
Here is my code:
year = int(input("Which year do you want to check? "))
if year%4==0:
if year0==0:
if year@0==0:
print("Leap year")
else:
print("Not leap year")
print("Leap year")
else:
print("Not leap year")
else:
print("Not leap year")
CodePudding user response:
If it is divided by 100 and 400 in your case, it will print that it is leap year.
Because it will go into the year0 and then into if year@0 and will print leap year. Then it will go to the next statement after the else of year@0 and it will print it again. (the parts between **)
if year0==0:
if year@0==0:
**print("Leap year")**
else:
print("Not leap year")
**print("Leap year")**
CodePudding user response:
Alternatively, you could also write this way to make it more succinct and easy to understand:
def is_leap(year):
return not(year % 4 and year % 100) or year % 400 == 0
print(is_leap(2000)) # True
print(is_leap(2024)) # True
print(is_leap(2022)) # False