r/learnpython • • Oct 13 '24

My if statement doesnt work

Can anyone find any errors here? Im new to python so I dont know what I did wrong as every answer is responded with “it has 30 days”

month = input("Enter a month: ") month = month.upper() print(month) if month == "SEPTEMBER"or"APRIL"or"JUNE"or"NOVEMBER": print("it has 30 days") elif month == "JANUARY"or"MARCH"or"MAY"or"JULY"or"DECEMBER"or"AUGUST": print("it has 31 days") elif month == "FEBRUARY": print("it has 28 days, but 29 on a leap year") else: print("That's not a month")

This is what the output is like Enter a month: a A It has 30 days

1 Upvotes

18 comments sorted by

View all comments

17

u/ssnoyes Oct 13 '24 edited Oct 13 '24

month == "SEPTEMBER" or "APRIL" does not mean the same thing as month == "SEPTEMBER" or month == "APRIL"

The first one means: check if the month is September. If it is, return True. If not, then return the string "APRIL". Any non-empty string is "truthy" in an if statement. So that first condition is always True.

You probably would benefit from learning about the in() operator.

15

u/Diapolo10 I write code for a living -- https://github.com/Diapolo10 Oct 13 '24

the in() operator

I know I'm being pedantic, but operators aren't functions so those parentheses kind of tick me off more than usual. You can't call in, unless you were talking about the __contains__-method but the name is different. I don't want OP to get confused.