How to use Or correctly with strings in an if statement

This is the function I wrote. If I enter Wednesday as the day of the week, the program will not be able to get to it to execute the print code. What is the correct syntax for this line of code for the environment to work correctly?

def day(dayOfWeek):
    if dayOfWeek == ("Monday" or "Wednesday"):
        print("Poetry: 6-7:15 in Chem 131")
+3
source share
3 answers

The expression ("Monday" or "Wednesday")in your code is always evaluated as "Monday". An operator oris a boolean orthat first tries if its first operand is evaluatedTrue . If so, it returns the first operand, otherwise it returns the second operand. Since it "Monday"is "trucy", your comparison is always compared to "Monday".

Use this instead:

if dayOfWeek in ("Monday", "Wednesday"):
    print("Poetry: 6-7:15 in Chem 131")
+8

, Sven, , , , , or, :

if (dayOfWeek == "Monday") or (dayOfWeek == "Wednesday"):
+3

==

if dayOfWeek == "Monday" or dayOfWeek == "Wednesday":
    print("Poetry: 6-7:15 in Chem 131")
0

Source: https://habr.com/ru/post/1787569/


All Articles