0
# take operand    
print('Select operator') 
operator = str(input('Choose between + or * or - or /'))
if operator in ("+", "*", "-", "/"):
pass
else:
print ('error')
operator = str(input('Choose between + or * or - or /'))

I am trying to make it check if the entered symbol is, in fact, one of these "+", "*", "-" or "/"

If they enter a letter, for example, I want it to return that they should only one of the symbols. It works with a lot of elifs but it also prints four times that the symbol is incorrect. I want to fix that.

1 Answers1

0

I think Georgy was correct and this is very similar to the answer they linked to above. However, you have a few other issues that you might be running into that might be confusing you. I think what you are looking for is something similar to this:

# take operand    
print('Select operator')
while True:
  operator = str(input('Choose between + or * or - or /'))
  if operator in ("+", "*", "-", "/"):
    break
  else:
    print('error')
    continue

print(operator)

You want to use while True because you want this to repeat until you reach an ending condition. You could also potentially set a variable and change the value within the while loop instead of doing break and continue. However, in this case, if you hit the ending condition (the user has typed in valid input), then you want to break out of the while loop. If not, you want to continue. I would also recommend this question and answer on stackoverflow: When to use "while" or "for" in Python

jmkoni
  • 463
  • 4
  • 14