eval SyntaxError: invalid syntax in python -


i want assign :

x0='123'     x1='123'     x2='123'     x3='123'     x4='123'     x5='123'     x6='123'     x7='123'     x8='123'     x9='123'     

i write code express can output of string 123 when input x1 or x8 .

for in range(0,10):     eval("x"+str(i)+"='123'")  traceback (most recent call last): file "<stdin>", line 2, in <module> file "<string>", line 1   x0='123'   ^ syntaxerror: invalid syntax 

how can way?

for dynamic execution of statements use exec statement.

>>> exec('y=3') >>> y 3 

eval usgae:eval(expression)

the expression argument parsed , evaluated python expression

eg:

>>> s=3 >>> eval('s==3') true >>> eval('s + 1') 4 >>> eval('s') 3 >>> eval('str(s)+"test"') '3test' 

Comments

Popular posts from this blog

javascript - Count length of each class -

What design pattern is this code in Javascript? -

hadoop - Restrict secondarynamenode to be installed and run on any other node in the cluster -