0

I'm trying to convert English into a python code, and here is how I do it:

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)

But then: the following error occurs:

Traceback (most recent call last):
  File "main.py", line 10, in <module>
    eval(pe)
  File "<string>", line 2
    dorito = "yum"
           ^
SyntaxError: invalid syntax
Charles Duffy
  • 280,126
  • 43
  • 390
  • 441

1 Answers1

1

eval doesn't work for assignment operation. Maybe, you should use exec in your case instead.

Read more about exec: https://docs.python.org/2.0/ref/exec.html

Aniket Tiratkar
  • 798
  • 6
  • 16