0

I'm trying to get pyton execute this, but it keeps showing EOL err, where is the mistake in here?

exec("a = \"def f(s):\n try:\n  exec(s)\n except: Exception\n print('Error')\"\nb = \"while True: f(input(''))\"\nexec(a)\nexec(b)")

This code is result of me trying to merge these two exec lines into 1, maybe there is better way to do it, there are those two exec lines:

exec("def f(s):\n try:\n  exec(s)\n except: Exception\n print('Error')")
exec("while True: f(input(''))")
  • 2
    If this kind of things is necessary, it's probably better to build the AST up piece-by-piece rather than exec it as a whole string. – o11c Jul 09 '18 at 01:23
  • 1
    Why does this need to be `exec`ed and not hard-coded? Programs should **never** execute inputted code, simple typos can cause catastrophic results. – Levi Lesches Jul 09 '18 at 01:31

1 Answers1

2

What you are trying to do smells horrible, Python-wise. But technically you have a multi-line string literal in your code, and it must be enclosed in triple quotation marks:

exec("a = '''def f(s):\n try:\n  exec(s)\n except: Exception\n print('Error')'''\nb = \"while True: f(input(''))\"\nexec(a)\nexec(b)")

There are some other errors in your code withing the string.

DYZ
  • 55,249
  • 10
  • 64
  • 93