【问题标题】:EOL error, where is the mistake?EOL 错误,错误在哪里?
【发布时间】:2018-07-09 00:36:27
【问题描述】:

我正在尝试让 pyton 执行此操作,但它一直显示 EOL err,这里的错误在哪里?

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

这段代码是我试图将这两条 exec 行合并为 1 的结果,也许有更好的方法来做到这一点,有这两条 exec 行:

exec("def f(s):\n try:\n  exec(s)\n except: Exception\n print('Error')")
exec("while True: f(input(''))")

【问题讨论】:

  • 如果这种事情是必要的,最好逐段构建AST,而不是作为一个完整的字符串执行。
  • 为什么这需要execed 而不是硬编码?程序应该从不执行输入的代码,简单的拼写错误会导致灾难性的后果。

标签: python python-3.x eol


【解决方案1】:

你正在尝试做什么smells horrible,Python 方面。但从技术上讲,您的代码中有一个多行字符串文字,它必须用三引号括起来:

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

您的代码中存在一些其他错误。

【讨论】:

    猜你喜欢
    • 2015-04-11
    • 1970-01-01
    • 2017-10-17
    • 1970-01-01
    • 1970-01-01
    • 2015-12-31
    • 2017-05-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多