2
if not operation == '/' or '*' or '+' or '-':
  print('not a valid answer, try again')
  operation = (input('Please enter what operation you would like to do, / is divide, * is multiply, + is plus and - is minus')
TheEagle
  • 5,808
  • 3
  • 11
  • 39
iidynamix
  • 31
  • 1
  • Can you put your code into a block to make it easier to see what is happening? – goalie1998 Jan 25 '21 at 01:57
  • @jarmod thats not the cause of the SytaxError – TheEagle Jan 25 '21 at 02:03
  • @frederic yes, you're right, thanks. *A* problem for sure, but not *the* problem. The OP's question title was a red herring. – jarmod Jan 25 '21 at 02:07
  • 1
    Caused by a typo, as per [@frederic's answer](https://stackoverflow.com/a/65878079/1431750) _(which should have just been a comment)_. But the reason it won't work after that fix, is coz the code doesn't do what OP thinks it does. For that see [Timur's answer](https://stackoverflow.com/a/65878040/1431750). – aneroid Jan 25 '21 at 02:07
  • @jarmod who spotted the ___real___ cause ? – TheEagle Jan 25 '21 at 02:07
  • Now who closed this question ? – TheEagle Jan 25 '21 at 02:10
  • 1
    The syntax error is coming from the unclosed parentheses. However, `not operation == '/' or '*' or '+' or '-'` is not doing what you think it is doing (when you correct the syntax error) – juanpa.arrivillaga Jan 25 '21 at 03:01
  • @iidynamix Please accept the answer that helped you most using they grey checkmark on the left of it ! – TheEagle Feb 20 '21 at 17:18

3 Answers3

3

operation = (input('Please enter what operation you would like to do, / is divide, * is multiply, + is plus and - is minus')

You forgot to add a second closing brace. In this case, just remove the opening brace.

Additionally, your if statement will always be True. Following code will work:

if not operation in ['/', '*', '+', '-']: # test if operation is one of /, *, + and -
  print('not a valid answer, try again')
  operation = input('Please enter what operation you would like to do, / is divide, * is multiply, + is plus and - is minus')
TheEagle
  • 5,808
  • 3
  • 11
  • 39
2

Did you mean something like this? Note the corrected syntax and logic. Use an infinite loop, prompt for the user input before you check that input. Exit from the loop (break) when operation belongs to the set of allowed values. Otherwise, repeat the loop and prompt for the user input again. Use f-strings or formatted string literals to print incorrect input (make the user error easier to see).

while True:
    operation = input('Please enter what operation you would like to do, / is divide, * is multiply, + is plus and - is minus: ')
    if operation in {'/', '*', '+', '-'}:
        break
    print(f'Not a valid answer: {operation}, try again')
Timur Shtatland
  • 12,024
  • 2
  • 30
  • 47
0

Each clause that is broken up by an or statement is its own Boolean.

So if not operation == '/' or '*' or '+' or '-': really represents four different Booleans:

  1. if not operation == '/'
  2. '*'
  3. '+'
  4. '-'

To correct this, you need to write each statement as its own conditional:

  1. if not operation == '/'
  2. if not operation == '*'
  3. if not operation == '+'
  4. if not operation == '-'

Or, strung together,

if not operation == '/' or not operation == '*' or not operation == '+' or not operation == '-'
Yehuda
  • 1,787
  • 2
  • 15
  • 49
  • The first part of what you were trying to explain is correct but the "strung together" version is wrong. (Check with an invalid `operation` like `x` or a valid one like `*` and you get `True` in both cases.) When you put them together like that, it should be joined with `and`'s, not `or`'s. Like this: `if not operation == '/' and not operation == '*' and not operation == '+' and not operation == '-': print('error')`. Also, you're missing a `==` before `'-'` at the end. – aneroid Jan 25 '21 at 13:21