0

I've only started learning to program a few days ago and I chose python as my starting language. I'm in the process of making a basic calculator with 4 operators to choose between. I want to make it so that if you input a character that is not one of these: + - * / then it would not try to calculate, but print 'Invalid operator' instead. The calculator works on 3 human inputs; a (first number), operator, b (second number).
Things I've already tried:

if not operator == '+' or '-' or '*' or '/'
    print('Invalid operator')
if not operator == ('+' or '-' or '*' or '/')
    print('Invalid operator')

So, only if the first operator is used will it not print 'Invalid operator', no matter the order. If any of the other three show 'Invalid operator' after the answer if printed. An answer on what I've done wrong and how to come about fixing my code would be greatly appreciated. Also an explanation on how things like this work for future reference would be cool too, since I haven't followed any online courses, I've just been using python documentation and stack overflow.

mhhabib
  • 2,975
  • 1
  • 15
  • 29
mackereldev
  • 43
  • 1
  • 8
  • "or" isn't distributive, you have to write "not (operator == '+' or operator == '-' or ...)" and so on or use "in" as shown by Manoj. – Michael Butscher Feb 05 '21 at 03:51

3 Answers3

4

You can use the in operator. Example:

if operator not in ['+', '-', '*', '/']:
    print("Invalid Operator")
2

In Python or and and is pretty basic. What or does is, the program executes the next line of code if one of your func is true. For an example:

eg: if the operator is '+' or '-' or '*', it knows that the line is true because it has + or - or *

What and does is it checks if all the variable are true. For an example: eg: if person has hand and legs and body and head, the person is a human. It is true only if all the vars are true. Get it? If you want more explanation, try to google it.

Pahan
  • 68
  • 1
  • 13
2

The best way to do this practically speaking is to use the in operator.

if not operator in ('+', '-', '*', '/'):
    print('Invalid operator')

If you really want to use or, you'd have to repeat the first half of the condition for every case:

if not operator == '+' or not operator == '-' or not operator == '*' or not operator == '/':
    print('Invalid operator')

You were also missing a : at the end of the first line of your if statement. You need to add the colon.

Ray Johns
  • 768
  • 6
  • 14
  • 1
    Oh, didn't realise i needed to repeat the not operator part for each or, good to know. And about the `:` i missed, that was just an error I made in the post. Thank you for the answer. – mackereldev Feb 05 '21 at 04:07
  • 1
    @TheSalmonn no problem! Just FYI, you've seen at least three uses of the `in` operator here: I used it to check membership in a tuple `('+', '-', '*', '/')`, the accepted answer checked a list `['+', '-', '*', '/']`, and a comment checked a string `"+-*/"`. All of these will work (they may have better or worse performance, depending on the number of options). As long as the object you're using `in` on has the `__contains__` method, it will work. https://docs.python.org/3/library/operator.html#operator.contains – Ray Johns Feb 05 '21 at 20:43