0

So I'm making a calculator in python 3.6, but there is an error message and my code is not working.

if(op == "+"):
    print(add(num1, num2))
if(op == "-"):
    print(sub(num1, num2))
if(op == "*"):
    print(mul(num1, num2))
if(op == "/"):
    print(dev(num1, num2))
if(op == "stop"):
    rng = "f"
elif(op != "+", or "-", or "*", or "/", or "stop"):
    print("Please enter a valid operation!")

When I run the code, it gives me an error saying invalid syntax and highlights the "or" part in the string. Error Picture. How do I fix this?

ayhan
  • 70,170
  • 20
  • 182
  • 203
  • I'd recommend looking up an example of `or` before using it, the commas are invalid. Remove them, after that you will won't get respected results because it separates conditions (ex: `"-"` by itself is always true). But you don't need those conditions, just make the other items into `elseif` and change your `elseif` to `else`. – Spencer Wieczorek Jan 25 '18 at 23:51
  • Why not say `op not in {'+', '-', '*', '/'', 'stop'}` ? – jpp Jan 26 '18 at 00:10
  • Or, possibly, `else` might suffice instead of `elif`. – jpp Jan 26 '18 at 00:11
  • @jp_data_analysis You would also want to add `elif` to the pervious conditions as well in that case. – Spencer Wieczorek Jan 26 '18 at 00:13
  • 1
    @SpencerWieczorek Agreed. I think OP needs to reconsider logic before worrying about syntax errors. Too often the message is "what's wrong?" instead of "how can this be done better?" – jpp Jan 26 '18 at 00:16
  • @RobotechGamer, have you considered putting your functions in a dictionary (see my answer)? This is cleaner and easier to maintain. – jpp Jan 26 '18 at 00:31
  • Python does not support the [Oxford comma](https://en.m.wikipedia.org/wiki/Serial_comma). – Klaus D. Jan 26 '18 at 00:58

5 Answers5

2

To answer your question it's a syntax error because or does not expect a comma. Remove those and it won't produce an error:

...
elif(op != "+" or "-" or "*" or "/" or "stop"):
    print("Please enter a valid operation!")

The problem with this is or is separates and applies a logical or to each condition separately as in: op != "+", "-", "*", and so on. A string value by itself that isn't empty will always be true, as such your condition will be true always. So you will want to add on: op != "-", op != "*", and so on. Even with that adding those there is still a logical error as op can only be a single value at a time. As such it will still always evaluate to true, so you likely mean to use and instead.

But that entire condition is really not necessary. Simply use elif on the pervious conditions:

if(op == "+"):
    print(add(num1, num2))
elif(op == "-"):
    print(sub(num1, num2))
elif(op == "*"):
    print(mul(num1, num2))
elif(op == "/"):
    print(dev(num1, num2))
elif(op == "stop"):
    rng = "f"
else:
    print("Please enter a valid operation!")
Spencer Wieczorek
  • 21,229
  • 7
  • 44
  • 54
1

You can use 'or' like this:

if 5 == 5 or 4 == 4:
    print("Correct!")
else:
    print("Incorrect!")

So, no commas. Also take out the brackets in the 'if' statement.

But in your case, use or like this:

if userInput != 5 or userInput != 6 or userInput != 7:
    print("Do Something ...")
else:
    print("Do Something ...")

I know, you have to write the something over and over again.

Also by the way, change your ors to ands because then your code won't run as expected.

1
elif(op != "+" and op != "-" and op != "*" and op != "/" and op != "stop"):
    print("Please enter a valid operation!")

This is the correct syntax. Turns out you can't use 'op!=' once followed by several or(s) in order to check all the conditions.

Naman Chikara
  • 164
  • 14
  • `op != "+" or op != "-"` will always be true. Please check your logic before posting answers. – Nathan Vērzemnieks Jan 26 '18 at 00:14
  • 1
    @NathanVērzemnieks Technically the OP is only asking to fix their syntax errors, rather than their logical errors. As such this answer is entirely valid. – Spencer Wieczorek Jan 26 '18 at 00:19
  • @SpencerWieczorek Giving the least possible help that is technically correct seems... not very helpful. It's very clear what is meant to happen here and it's equally clear that this answer won't accomplish that. – Nathan Vērzemnieks Jan 26 '18 at 00:30
  • 1
    @NathanVērzemnieks Make no mistake I'm not considering this a good answer by any means. Just that it answers the question. – Spencer Wieczorek Jan 26 '18 at 00:45
  • 1
    @RosárioPereiraFernandes Thanks mate, It was my first answer so I'll take care of it in future thanks for pointing out. – Naman Chikara Jan 26 '18 at 00:49
  • 1
    @NathanVērzemnieks I was just trying to answer point-to-point, clearly, he's a beginner(so am I) and he wanted help for fixing that error, I tried to help him with that by answering him what the correct syntax is. – Naman Chikara Jan 26 '18 at 00:52
  • @NamanSingh I understand! And I'm trying to help you give better answers. Your answer would stop the syntax error but leave another problem, that the code will print the request for a valid operation every time the user has entered anything besides "stop". Especially as a beginner it's important that you make sure your suggestions work well. – Nathan Vērzemnieks Jan 26 '18 at 01:23
  • 1
    @NathanVērzemnieks Alright, thanks, I'll keep that in mind. – Naman Chikara Jan 26 '18 at 15:33
0

Your code should include "and !=" (which means not equal to) instead of ", or" This code will do the job:

if(op == "+"):
    print(add(num1, num2))
if(op == "-"):
    print(sub(num1, num2))
if(op == "*"):
    print(mul(num1, num2))
if(op == "/"):
    print(dev(num1, num2))
if(op == "stop"):
    rng = "f"
elif(op != "+" and op != "-" and op != "*" and op != "/" and op !="stop"):
    print("Please enter a valid operation!")
0

This, in my opinion, is a more pythonic solution.

op_map = {'+': add, '-': sub, '*': mul, '/': dev}

if op in op_map:
    print(op_map[op](num1, num2))

elif op == 'stop':
    rng = 'f'

else:
    print('Please enter a valid operation!')
jpp
  • 159,742
  • 34
  • 281
  • 339