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
0

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

4 Comments

Feel free to downvote, but please tell me reason for downvote.
I don't see why it would be downvoted; the code works correctly using exec: ideone.com/1A2aXG
(on the other hand, questions that are duplicative shouldn't be answered in the first place -- see the Answer Well-Asked Questions section of How to Answer)
@CharlesDuffy Ohh, I see. That makes sense.