输入() 导致"EOL while scanning string literal"错误



我目前正试图在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!")

当期望字符串作为输入时,您希望使用raw_inputinput的计算结果为python表达式。

为什么你认为while answer == (<<Enter>>)在Python中有任何意义?我建议你做一个Python教程,这样你就可以掌握语法了。

如果我理解你想要什么,你可以删除那一行:

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

input的调用会停止任何其他操作,直到按下回车键。

我很确定你实际上并没有使用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()在Python3中被删除,而老的raw_input()取代了它。

最新更新