r/learnpython • u/vtuti • 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
1
u/[deleted] Oct 13 '24
I had the same problem earlier.
Your mistake is in using the "or" operator or is used to separate different conditions. everytime you use it you should write a whole new condition.
in this case, take the line
^^^
after "or" you have just written "APRIL", the python interpreter is taking "APRIL" as one condition and not checking whether month is equal to september or april or june or november.
the correct version would be
in this version after or there is a condition, in your code there is a string after "or". In python, a string alone as a condition just checks whether or not the string has characters. it returns true if it has any characters. Thus, your code was probably outputting "it has 30 days"
Even this isnt the fully corrected version of the code. it would look more like this if you know how to use "in".
this checks whether the month inputted by the user is in the list with those months which have 30 days. if it is, which we will assume for this case as true, it will return True, making the condition true.
also i request you to write code properly when you are posting on reddit use code blocks or tabs or 3 backticks when posting code.
Thank you