Is there a way to do effectively do:
if operator != ('+' or '-' or '*' or '/'):
without having to do
operator != '+' and operator != '-' and operator != '*'
Is there a way to do effectively do:
if operator != ('+' or '-' or '*' or '/'):
without having to do
operator != '+' and operator != '-' and operator != '*'
use the in
operator
if operator not in ('+', '-', '/')
Effective to read solution would be: if operator in ('+', '-', '*', '/')
or simply if operator in '+-*/'
(thanks @kindall) which is like looking for a char in a string. I personally find it less readable though.
Otherwize you use a dictionnary (for instance, to bind a function to each operator) and just use exceptions:
ops = {'+': 'plus', '-': 'minus', '*': 'times', '/': 'div'}
operator = '/'
try:
print ops[operator]
except KeyError:
print "Unknow operation (%s)" % (operator)