Python initialize strings as variable -


i have calculation schema string

calc = "((k+m+46)/2)" 

and strings containing variable like

m = 2 k = m*2 

all strings. want initialize them python. goal calculate calculating schema varible values.

calc should return 26

edit:

i try exec vars in cases have exec k = m*2 @ first , error m not defined!

#!/usr/bin/python  m = 2 k = m*2 calc = "((k+m+46)/2)"  result = eval(calc)  print result 

result

26 

Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -