Search code examples
pythonpython-3.xeol

EOL error, where is the mistake?


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(''))")

Solution

  • 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.