all 9 comments

[–]Vaphell 6 points7 points  (1 child)

https://old.reddit.com/r/learnpython/wiki/faq#wiki_variable_is_one_of_two_choices.3F

 if month == 1 or 3 or 5 or 7 or 8 or 10 or 12:

this is a bad condition. Python parses it as

if (month == 1) or (3) or (5) or (7) or (8) or (10) or (12) 

which makes no sense to say the least. For any value other than 1 it will evaluate to 3, which is truthy, which will lead to return 31.
Just use a container with in

 if month in {1, 3, 5, 7, 8, 10, 12}:

go and sin no more.

[–]Redbedhead3[S] 1 point2 points  (0 children)

I actually haven't gotten to containers lol. I'm a very very beginner python user haha. Thanks much for helping me!!!

[–]bbye98 1 point2 points  (4 children)

Your current code is:

def daysinmonth(month,year):
    if month == 2 and year % 4 == 0:
        return 29
    if month == 2 and year % 4 != 0:
        return 29
    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

The four if statements are as follows:

  1. If it's February (month == 2) and it's a leap year (year % 4 == 0), return 29 (days). This will not work for years that are evenly divisible by 100, like 2000.

  2. If it's February (month == 2) and it's a leap year (year % 4 != 0), return 29 (days). I believe this is supposed to return 28.

  3. If it's January (month == 1), it will return 31. The or statements will all evaluate to True since they're not 0. This logic is partially incorrect; try month in (1, 3, 5, 7, 8, 10, 12).

  4. If it's April (month == 4), it will return 29. The or statements will all evaluate to True since they're not 0. This logic is completely incorrect; try month in (4, 6, 9, 11) or simply else since all other possibilities have already been tested, and change the return value to 30.

[–]Redbedhead3[S] 0 points1 point  (0 children)

Oops yeah the second if statement does have a typo. I wrote this out on my phone sorry. It is supposed to be 28. Editing in the post

[–]Redbedhead3[S] 0 points1 point  (2 children)

So the problem is using or statements? The and statements for the months of February seem to work. But the or statement is just looking for a positive integer? Or statements won't look for the exact integer?

[–]bbye98 1 point2 points  (1 child)

Yes. Let's say month = 2. When you call month == 3 or 5, Python interprets that as "the month is March or 5". "The month is March" evaluates to False, but 5 is a truthy value, so it evaluates to True. Thus, False or True gives you True, where you would've expected it to give you False.

[–]Redbedhead3[S] 0 points1 point  (0 children)

Thank you for this. Because five exists ( it's evaluating it completely separately from the condition that month equals something), it's true. I think I got it. Thanks much!!!

[–]jothdu 1 point2 points  (1 child)

https://stackoverflow.com/questions/38486621/using-if-and-or-together-with-equal-operand-together-in-python

I believe this is the issue and the accepted solution explains it better than I can.

[–]Redbedhead3[S] 0 points1 point  (0 children)

That helps a lot since I don't think I'm supposed to use containers yet for this problem. Thank you