0

leap_year

Why this code returns "None": def leap(year): if year % 4 == 0: if year % 100 != 0 or year % 400 == 0: return True else: return False print(leap(1900))

21st Sep 2021, 10:34 PM
Bekir
Bekir - avatar
3 Answers
+ 3
Hi Bekir! That's because your inner if statement doesn't have an else statement to return anything since it's a false statement. But, always remember that not all years that divided by 100 and 4 are leap years(1900,2700,3300....). So, your code needs to return False here. You can try these ways to make it as a valid code. 1. For years like 1900, it should return False. For that, you can add an else statement to the inner if statement. So, it will check all conditions and return you a proper output. if(): if(): else: else: 2. You can use one if-else statement with logical operators. It needs to be like this, if year%4 ==0 and year % 100 != 0 or year %400 ==0: return True else: return False
22nd Sep 2021, 1:47 AM
Python Learner
Python Learner - avatar
+ 3
Because 1900%100 = 0 but 1900%400 would not be zero it has one part of it but hasn't another part to be True so it would be None but if you change function's parameter to an odd number would be False Or if you change year%400!=0 it would be True for 1900
21st Sep 2021, 10:44 PM
Amirreza
Amirreza - avatar
0
Thanks Python Learner.
22nd Sep 2021, 7:27 AM
Bekir
Bekir - avatar