【问题标题】:input() causing "EOL while scanning string literal" errorinput() 导致“扫描字符串文字时 EOL”错误
【发布时间】:2015-02-17 16:58:30
【问题描述】:

我目前正在尝试在 Python 3.4.7 中编写一个块,如果输入的材料是 Enter,则该块会运行。但是当我按 Enter 时,它会显示以下错误消息:

SyntaxError: EOL while scanning string literal

一些示例代码:

answer = input("to find out your result, press enter.")
# Problem is here, I don't know what kind of sign or Python rule I'm not following
while answer == (<<Enter>>):
    print("You are the father!")

【问题讨论】:

  • 对input 的调用不会结束,直到您按 Enter。您只需要answer = input(...); print(...)。 while answer == (&lt;&lt;Enter&gt;&gt;): 毫无意义。
  • 包含错误,而不是仅仅说“类似 EOL 扫描文字”会更有帮助。

标签: python python-3.x input syntax


【解决方案1】:

当期望字符串作为输入时,您想使用raw_input。 input 计算为 Python 表达式。

【讨论】:

  • 这仅适用于 Python 2。问题标题指定这是 Python 3。
  • @sweeneyrod 问题标题指定(指定)不存在的 Python 3.4 .7(大约一周后发布 3.4.3)。我认为提问者不知道他使用的是什么版本。
【解决方案2】:

为什么你认为while answer == (&lt;&lt;Enter&gt;&gt;) 在 Python 中意味着什么?我建议你做一个 Python 教程,这样你就可以掌握语法的窍门。

如果我明白你想要什么,你可以删除该行:

answer = input("to find out your result, press enter.")
print("You are the father!")

对input 的调用会阻止其他任何事情发生,直到按下回车键。

【讨论】:

    【解决方案3】:

    我很确定您实际上并没有使用 Python 3(除非您来自 2017 年,那时我猜 Python 3.4.7 可能会发布)。 Python 2 中input() 的输入被执行(input(prompt) = eval(raw_input(prompt))),当它只是一个空字符串时,它会导致SyntaxError:

    Python 2.7.9 (default, Dec 10 2014, 12:28:03) [MSC v.1500 64 bit (AMD64)] on win32
    Type "help", "copyright", "credits" or "license" for more information.
    >>> input('Hello?')
    Hello?
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "<string>", line 0
    
        ^
    SyntaxError: unexpected EOF while parsing
    

    使用raw_input() 通常是您想要的。旧的 input() 在 Python 3 中被删除,旧的 raw_input() 取而代之。

    【讨论】:

    • 我来自 2017 年 ;)
    猜你喜欢
    • 1970-01-01
    • 2015-04-30
    • 1970-01-01
    • 2020-02-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多