尝试评估包含赋值的代码块的语法错误
syntax error trying to eval code block containing assignment
我正在尝试将英文转换为 python 代码,我是这样做的:
english.eng:
the following line is a comment: This is a demo of english programming language
set a new variable dorito as "yum"
if dorito is "yum" then do
print in the terminal ("dorito is")
main.py:
f = open("english.eng", "r")
pe = f.read()
pe = pe.replace("the following line is a comment:", "#")
pe = pe.replace("as", "=")
pe = pe.replace("set a new variable ", "")
pe = pe.replace(" is ", " == ")
pe = pe.replace(" then do", ":")
pe = pe.replace("print in the terminal ", "print")
print(pe)
eval(pe)
但是随后:出现以下错误:
Traceback (most recent call last):
File "main.py", line 10, in <module>
eval(pe)
File "<string>", line 2
dorito = "yum"
^
SyntaxError: invalid syntax
eval
不适用于赋值操作。也许,您应该在您的案例中使用 exec
。
阅读更多关于 exec
的信息:https://docs.python.org/2.0/ref/exec.html
我正在尝试将英文转换为 python 代码,我是这样做的:
english.eng:
the following line is a comment: This is a demo of english programming language
set a new variable dorito as "yum"
if dorito is "yum" then do
print in the terminal ("dorito is")
main.py:
f = open("english.eng", "r")
pe = f.read()
pe = pe.replace("the following line is a comment:", "#")
pe = pe.replace("as", "=")
pe = pe.replace("set a new variable ", "")
pe = pe.replace(" is ", " == ")
pe = pe.replace(" then do", ":")
pe = pe.replace("print in the terminal ", "print")
print(pe)
eval(pe)
但是随后:出现以下错误:
Traceback (most recent call last):
File "main.py", line 10, in <module>
eval(pe)
File "<string>", line 2
dorito = "yum"
^
SyntaxError: invalid syntax
eval
不适用于赋值操作。也许,您应该在您的案例中使用 exec
。
阅读更多关于 exec
的信息:https://docs.python.org/2.0/ref/exec.html