osdir.com


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Flag control variable


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