Python 解析时出现意外的 EOF
这是我的 python 代码。有人可以告诉我有什么问题吗?
while 1:
date=input("Example: March 21 | What is the date? ")
if date=="June 21":
sd="23.5° North Latitude"
if date=="March 21" | date=="September 21":
sd="0° Latitude"
if date=="December 21":
sd="23.5° South Latitude"
if sd:
print sd
发生的事情是这样的:
>>>
Example: March 21 | What is the date?
Traceback (most recent call last):
File "C:\Users\Daniel\Desktop\Solar Declination Calculater.py", line 2, in <module>
date=input("Example: March 21 | What is the date? ")
File "<string>", line 0
^
SyntaxError: unexpected EOF while parsing
>>>
Here's my python code. Could someone show me what's wrong with it.
while 1:
date=input("Example: March 21 | What is the date? ")
if date=="June 21":
sd="23.5° North Latitude"
if date=="March 21" | date=="September 21":
sd="0° Latitude"
if date=="December 21":
sd="23.5° South Latitude"
if sd:
print sd
And Here's what happens:
>>>
Example: March 21 | What is the date?
Traceback (most recent call last):
File "C:\Users\Daniel\Desktop\Solar Declination Calculater.py", line 2, in <module>
date=input("Example: March 21 | What is the date? ")
File "<string>", line 0
^
SyntaxError: unexpected EOF while parsing
>>>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
使用
raw_input
而不是input
:)参考
注意:这仅适用于 Python 2。 Python 3 中,
raw_input()
已变为普通的input()
,并且 Python 2input()
已被删除。Use
raw_input
instead ofinput
:)Reference
Note: this is only for Python 2. For Python 3,
raw_input()
has become plaininput()
and the Python 2input()
has been removed.首先缩进!。这将解决您的
SyntaxError
问题。除此之外,您的程序中还存在其他一些问题。
当您想要接受字符串作为输入时,请使用
raw_input
。input
仅接受 Python 表达式,并对其执行eval
。您在脚本中使用了某些 8 位字符,例如
0°
。您可能需要使用# -*-coding:latin-1 -*-
行(通常称为coding-cookie)在脚本顶部定义编码。此外,在进行 str 比较时,规范化字符串并进行比较。 (人们使用 lower() it)这有助于为用户输入提供很少的灵活性。
我还认为阅读 Python 教程可能会对您有所帮助。 :)
示例代码
Indent it! first. That would take care of your
SyntaxError
.Apart from that there are couple of other problems in your program.
Use
raw_input
when you want accept string as an input.input
takes only Python expressions and it does aneval
on them.You are using certain 8bit characters in your script like
0°
. You might need to define the encoding at the top of your script using# -*- coding:latin-1 -*-
line commonly called as coding-cookie.Also, while doing str comparison, normalize the strings and compare. (people using lower() it) This helps in giving little flexibility with user input.
I also think that reading Python tutorial might helpful to you. :)
Sample Code
我遇到了这个错误,因为一行中缺少右括号。
我一开始就遇到了这样一行的问题:
语法无效(、line ...)?
在我的脚本的末尾。
我删除了该行,然后收到 EOF 消息。
I had this error, because of a missing closing parenthesis on a line.
I started off having an issue with a line saying:
invalid syntax (<string>, line ...)?
at the end of my script.
I deleted that line, then got the EOF message.
我使用以下代码来获得 Python 2 和 3 兼容性
I'm using the follow code to get Python 2 and 3 compatibility
虽然 @simon 的答案在 Python 2 中最有帮助,但 Python 3 中不存在
raw_input
。我建议执行以下操作以确保您的代码在 Python 2 和 Python 3 中同样有效:首先, pip install future:
第二:从 future.builtins 导入输入
对于上面列出的具体示例:
While @simon's answer is most helpful in Python 2,
raw_input
is not present in Python 3. I'd suggest doing the following to make sure your code works equally well in Python 2 and Python 3:First, pip install future:
Second: import input from future.builtins
And for the specific example listed above:
我试图一般性地回答,与这个问题无关,当您将语法分成两半并忘记另一半时,通常会发生此错误。就像我的例子一样:
因为 python 正在搜索 a
但它遇到了 EOF(文件结束),因此出现错误。看看您是否可以在代码中找到任何不完整的语法。
I'm trying to answer in general, not related to this question, this error generally occurs when you break a syntax in half and forget the other half. Like in my case it was:
since python was searching for a
but it encountered an EOF (End Of File), hence the error. See if you can find any incomplete syntax in your code.
我遇到了同样的事情,我弄清楚了问题是什么。当我们使用方法输入时,我们应该输入的响应应该用双引号引起来。就像你的行一样
date=input("示例:3 月 21 日 | 日期是几号?")
当控制台出现提示时,您应该输入“12/12/2015” - 请注意
"
内容这样,它会将其作为字符串并按预期进行处理,我不确定这是否是此input
方法的限制 - 但它是这样工作的,希望它有帮助。
i came across the same thing and i figured out what is the issue. When we use the method input, the response we should type should be in double quotes. Like in your line
date=input("Example: March 21 | What is the date? ")
You should type when prompted on console "12/12/2015" - note the
"
thing before and after. This way it will take that as a string and process it as expected. I am not sure if this is limitation of thisinput
method - but it works this way.Hope it helps
在第一个 if 语句之后,不要输入“if”,而是输入“elif”,然后它应该可以工作。
前任。
After the first if statement instead of typing "if" type "elif" and then it should work.
Ex.
您可以尝试使用普通的
input
命令像平常一样为 python 编写代码。然而,诀窍是在编程开始时添加命令input=raw_input
。现在您所要做的就是禁用(或启用),具体取决于您是在 Python/IDLE 还是终端中运行。您只需在需要时添加“#”即可完成此操作。
关闭以在 Python/IDLE 中使用
,当然打开以在终端中使用。
我不确定它是否总是有效,但它是简单程序或脚本的可能解决方案。
What you can try is writing your code as normal for python using the normal
input
command. However the trick is to add at the beginning of you program the commandinput=raw_input
.Now all you have to do is disable (or enable) depending on if you're running in Python/IDLE or Terminal. You do this by simply adding '#' when needed.
Switched off for use in Python/IDLE
And of course switched on for use in terminal.
I'm not sure if it will always work, but its a possible solution for simple programs or scripts.
检查您的编译器的版本。
Check the version of your Compiler.