I have code that generates random math examples. The program asks the user what character he wants in the example (+, -, /, *) . And further the condition on whether he wrote a mat at all. operator, and not any "йсмйСЙ234ВЙ2Ф" . "йсмйСЙ234ВЙ2Ф" I tested, and noticed: when I write exactly mat. operator, he from the test displays, as if I entered not what is written in the condition. Here is the code snippet:

 want = input("Выберите знак действия для вашего примера (+, -, /, *): ") if want != "+" or "-" or "/" or "*": print("!!! | Вы должны ввести ЗНАК действия!") else: 
  • 2
    A non-empty string is considered true. The or operator returns True if there is True on the left or right. Therefore, when you write чтототам or "-" , the whole expression is automatically considered True (because the string to the right is True) and the condition is triggered. - andreymal

3 answers 3

Instead of or should be and. Otherwise, in any case, an error will be displayed.

 want = input("Выберите знак действия для вашего примера (+, -, /, *): ") if ((want != "+") and (want != "-") and (want != "/") and (want != "*")): print("!!! | Вы должны ввести ЗНАК действия!") 

    Try this:

     if want not in ["+","-", "/","*"]: print("!!! | Вы должны ввести ЗНАК действия!") 

      You need to read about the priority of the operators. In the design

       if want != "+" or "-" or "/" or "*": 

      you check that the variable want not equal to "+" or the string "-" has a true value or the string "/" has a true value or the string "*" has a true value. Since a non-empty string always has a true value, if will always be executed.