I am writing code to solve a basic problem:
def is_leap(year):
leap = False
if year % 4 == 0:
if year % 100 == 0 and year % 400 != 0:
leap = False
if year % 100 == 0 and year % 400 == 0:
leap = True
else:
leap = True
return leap
print(is_leap(2100))
My question is with regards to the inner most scope; specifically for the second 'if statement'- I know this must be an 'elif' (and if we don't change it, our answer will be wrong) but I don't see why this is the case. In my IDE, if I leave the code as is above, the 'leap=False' for the first if statement greys out.
Even though I understand if vs elif generally, why can we not just have both as 'if statements'? Because (in this case) based on the logic, for any input we use as an argument, only one of the two if statements will be executed right?
Aucun commentaire:
Enregistrer un commentaire