-1

I understand that when typing an invalid operator for the first time the while loop begins but after I type a valid operator after the loop begins, it doesn't seem to exit out of it.

Is it because I have multiple conditions , since it actually works if I only have one condition?

numb_1 = float(input("Enter the first number:"))
operator = input("Enter an operator:")
numb_2 = float(input("Enter the second number:"))

while operator != ("+") or operator != ("-")  or operator != ("*") or operator != ("/"):
    print("Invalid operator please try again.")
    operator = input("Enter an operator:")

if operator == ("+"):
    print(numb_1 + numb_2)

elif operator == ("-"):
    print(numb_1 - numb_2)

elif operator == ("*"):
    print(numb_1 * numb_2)

elif operator == ("/"):
    print(numb_1 / numb_2)    
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
Dylan
  • 19
  • 2
  • 2
    can you please post your code? – Leonardo Scotti Jan 31 '21 at 10:38
  • yes I have just posted it thank you – Dylan Jan 31 '21 at 10:46
  • 1
    You need `and` not `or`... Think what happens if you input `"+"`. The condition `operator != ("-")` is still `True` so the condition holds. You want to check that the input is different than ***all*** operators so you need to use `and` – Tomerikoo Jan 31 '21 at 10:54
  • Does this answer your question? [Asking the user for input until they give a valid response](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) – Tomerikoo Jan 31 '21 at 13:00

2 Answers2

1

A little code snippet might help to understand your exact question. An operator is typically something like '+', '-', '/', '%', '*', '&', ... etc. I'm not clear what you mean with an "invalid operator". Since you are referring to a while loop, and expecting it to exit out, I guess you are referring to a 'condition'?

while(<condition == true>):
  <do something>

Make sure your condition actually once becomes not True (False), and your code block should jump out of your while block.


Updating now that the original post has the code.

The problem is here:

operator != ("+") or operator != ("-")  or operator != ("*") or operator != ("/")

Since you are using a logical 'or' operator, the moment 1 of these conditions is true, the entire condition is true. See truth table of OR.

Which means... if you enter +, the condition "operator != ("-")" is true... => the while loop condition is true, and you stay in the loop forever.

A better ways would be:

operator = input("Enter an operator:")
while operator not in ["+", "-", "*", "/"]:
  print("Invalid operator please try again.")
  operator = input("Enter an operator:")
  • yes Ive made the condition true once to activate the loop but when I make the condition false it doesnt exit the loop. I wonder if it's because I have multiple conditions since the loop works just fine with one condition. – Dylan Jan 31 '21 at 10:53
  • Thanks for putting the code. Your problem is that you were checking multiple conditions with a logical or. Which means, the moment 1 condition is true, the entire series of conditions is true. So if someone enters '+', a valid character, your condition ' != '-' ' is true. Hence the condition is still true, and you keep looping! – Cedric Van Labeke Jan 31 '21 at 12:51
0

Try this:

numb_1 = float(input("Enter the first number:"))

while True:
    operator = input("Enter an operator:").strip()
    if operator in["+", "-", "/", "*"]:
         break
    else:
        print("Invalid operator please try again.")

numb_2 = float(input("Enter the second number:"))
Leonardo Scotti
  • 1,069
  • 8
  • 21