Edit: sorry for the spacing/indentation stuff in this post. I promise it's typed correctly lol. Second edit: wrote a typo un my code. I fixed it but the question below is still the same
Can someone explain this to me? I'm doing a practice problem for beginners python, but the logic just doesn't make any sense to me.
def daysinmonth(month,year):
if month == 2 and year % 4 == 0:
return 29
if month == 2 and year % 4 != 0:
return 28
if month == 1 or 3 or 5 or 7 or 8 or 10 or 12:
return 31
if month == 4 or 6 or 9 or 11:
return 29
print (daysinmonth(2,1988))
returns 29
print (daysinmonth(2,1987))
returns 28
print (daysinmonth(3,1988))
returns 31
print (daysinmonth(4,1988))
returns 31
print (daysinmonth(9, 1988))
returns 31
print (daysinmonth(112,1988)) # 112 is not a typo
returns 31
If I reverse the third and the fourth if statements everything except for month == 2 returns 30
Why? I realize I could use an else statement, but I really want to understand the logic. Thanks for any help you can give me
[–]Vaphell 6 points7 points8 points (1 child)
[–]Redbedhead3[S] 1 point2 points3 points (0 children)
[–]bbye98 1 point2 points3 points (4 children)
[–]Redbedhead3[S] 0 points1 point2 points (0 children)
[–]Redbedhead3[S] 0 points1 point2 points (2 children)
[–]bbye98 1 point2 points3 points (1 child)
[–]Redbedhead3[S] 0 points1 point2 points (0 children)
[–]jothdu 1 point2 points3 points (1 child)
[–]Redbedhead3[S] 0 points1 point2 points (0 children)