-3
operator = input("Please select one option: add/subtract/multiply/divide: ")
if operator != "add" or operator != "subtract" or operator != "multiply" or operator != "divide":
    print("The option you chose (" + operator + ") is not valid\nPlease try this program again.")
    sys.exit()

Result:

Please select one option: add/subtract/multiply/divide: add                                                                                                                                 
The option you chose (add) is not valid                                                                                                                                                     
Please try this program again.
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Reiju
  • 1
  • 1
  • 1
    Think about it: if you input `"add"`, then `operator != "add"` will be `False`, but `operator != "subtract"` will be `True`, `operator != "multiply"` will also be `True`, so as `operator != "divide"`. You probably need to rethink the logic. – Péter Leéh Nov 07 '20 at 08:13

1 Answers1

1

You're confusing how boolean algebra works.

operator != "add" or operator != "subtract" or operator != "multiply" or operator != "divide"

This will return true if operator is not either of add/subtract/multiply/divide.

So when you enter add, operator != "add" returns false, but the next one - operator != "subtract" returns true. Since it is an or operator, only one of all the expressions need to return true, for the whole expression to return true.

You should instead do-

operator != "add" and operator != "subtract" and operator != "multiply" and operator != "divide"

In simple english, that reads "return true if operator is none of add/subtract/multiply/divide"

Or more simply, using the in operator-

operator not in ["add", "subtract", "multiply", "divide"]

Which in simple english, reads "return true if operator is not an element in the list ["add", "subtract", "multiply", "divide"]"

Chase
  • 5,315
  • 2
  • 15
  • 41