0

I'm trying to use this code to create a while loop from a string, but it's not working. This is the string that I call with the eval() function:

    'while True:\n\vprint("What role do you want?")\n\vAnswer = Reformat(raw_input())\n\vif Answer in RoleArray:\n\v\vPlayArray[0].append(Answer)\n\vif Answer == "Mafia":\n\v\v\vMafia.append(User)\n\v\velif Answer == "Sheriff":\n\v\v\vSheriff = User\n\v\velif Answer == "Doctor":\n\v\v\vDoctor = User\n\v\vdel RoleArray[RoleArray.index(Answer)]\n\velse:\n\v\vprint("That is not a role. Please check you spelling. [Mafia/Sheriff/Doctor/Townsperson]")'

And this is the error that shows up:

    Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
eval('while True:\n\vprint("What role do you want?")\n\vAnswer = Reformat(raw_input())\n\vif Answer in RoleArray:\n\v\vPlayArray[0].append(Answer)\n\vif Answer == "Mafia":\n\v\v\vMafia.append(User)\n\v\velif Answer == "Sheriff":\n\v\v\vSheriff = User\n\v\velif Answer == "Doctor":\n\v\v\vDoctor = User\n\v\vdel RoleArray[RoleArray.index(Answer)]\n\velse:\n\v\vprint("That is not a role. Please check you s')
File "<string>", line 1
while True:
    ^
SyntaxError: invalid syntax

There is an alternate way to execute this code properly, but I would like to know why it isn't working. Thanks.

9
  • 4
    Why are using \v? Commented Dec 6, 2016 at 0:48
  • 3
    You want exec, not eval, I think. Commented Dec 6, 2016 at 0:48
  • 2
    Also, for whatever you're doing, I strongly recommend against using eval()\exec() Commented Dec 6, 2016 at 0:49
  • 2
    you need \t instead of \v Commented Dec 6, 2016 at 0:50
  • 3
    As another side note, this sounds a bit like an X/Y problem. Don't ask how to fix your solution. Ask about how to solve your actual problem. Commented Dec 6, 2016 at 0:56

2 Answers 2

2

The most likely culprit is both your use of \v instead of \t for tabs and your use of eval. You need to fix both:

Fixing both removes the syntax error:

>>> exec("while True:\n\tprint('yup')\n\tbreak")
yup

Using the vertical tab \v causes a syntax error:

>>> exec("while True:\n\vprint('yup')\n\vbreak")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<string>", line 2
    ♂print('yup')
    ^
SyntaxError: invalid syntax

And so does using eval, which is intended only for expressions:

>>> eval("while True:\n\tprint('yup')\n\tbreak")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<string>", line 1
    while True:
        ^
SyntaxError: invalid syntax

See this answer for an explanation of the differences between eval and exec.

It's possible you have other syntax errors as well, but I'm not going to try to debug that very long line.

Additionally, you should strongly reconsider your use of the dynamic code consuming functions like eval and exec. Even if your code's intended use doesn't present any security risk, they make things much more difficult to debug and maintain. The cases where they can't be avoided are exceedingly rare.

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

2 Comments

The use of \v is working fine, but I will keep \t in mind. Thx.
@GreenHawk1220 Hmm... what OS and python version are you using? That's odd that it would cause a syntax error for me and not you.
0

After veiwing comments, I realize that I was being really dumb as to not use exec() instead of eval(), since one gives me a variable, and the other gives me a variable. Thank you, Blorgbeard!

4 Comments

Are you sure it's working? Whenever I run it using exec() I get a SyntaxError.
@leaf You may be seeing a similar issue that I'm getting. Are you using \v or \t for the tabs?
@skrrgwasme I am using \v and it does not work. In fact, when I print the escape character in the idle, it prints a strange empty box.
Yeah, it doesn't. I was misreading it. Exec() with \t solved the problem. Thx guys.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.