Flag control variable

Tim Chase python.list at tim.thechases.com
Tue Feb 11 13:32:54 EST 2014


On 2014-02-11 10:16, luke.geelen at gmail.com wrote:
> when expandig the script to multiple calcs i got a problem
> >>> a = 32
> >>> c = 51
> >>> sign = *
> 
> File "<stdin>", line 1
>     sign = *
>            ^
> SyntaxError: invalid syntax
> 
> is there a way of adding * without quoting marks, because if you do
> it just soms the arguments -- 

You want to store the actual operation.  The "operator" module makes
this fairly easy, so you can do something like

  import operator as o
  operations = {
    "*": o.mul,
    "+": o.add,
    "/": o.div,
    "-": o.sub,
    }

  a = 32
  c = 51
  operation = operations["*"]
  print(operation(a,c))

-tkc





More information about the Python-list mailing list