Python 解析时出现意外的 EOF

发布于 2024-10-18 15:28:10 字数 726 浏览 3 评论 0原文

这是我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

土豪我们做朋友吧 2024-10-25 15:28:10

使用 raw_input 而不是 input :)

如果您使用input,那么您的数据
类型被解释为 Python
表达式这意味着您
最终得到了天啊知道什么类型
目标变量中的对象,以及
有各种各样的例外情况
可以生成的。所以你应该
不要使用输入,除非您要输入
用于临时测试的东西,以
只能由了解的人使用
关于 Python 表达式的一些知识。

raw_input 始终返回一个字符串
因为,哎呀,这就是你总是这样的
输入...然后您就可以轻松地
将其转换为您的特定类型
想要,并抓住具体的
可能发生的异常情况。希望
有了这个解释,这是一个
不用想就知道你应该做什么
使用。

参考

注意:这仅适用于 Python 2。 Python 3 中,raw_input() 已变为普通的 input(),并且 Python 2 input() 已被删除。

Use raw_input instead of input :)

If you use input, then the data you
type is is interpreted as a Python
Expression
which means that you
end up with gawd knows what type of
object in your target variable, and a
heck of a wide range of exceptions
that can be generated. So you should
NOT use input unless you're putting
something in for temporary testing, to
be used only by someone who knows a
bit about Python expressions.

raw_input always returns a string
because, heck, that's what you always
type in ... but then you can easily
convert it to the specific type you
want, and catch the specific
exceptions that may occur. Hopefully
with that explanation, it's a
no-brainer to know which you should
use.

Reference

Note: this is only for Python 2. For Python 3, raw_input() has become plain input() and the Python 2 input() has been removed.

纵情客 2024-10-25 15:28:10

首先缩进!。这将解决您的 SyntaxError 问题。

除此之外,您的程序中还存在其他一些问题。

  • 当您想要接受字符串作为输入时,请使用raw_inputinput 仅接受 Python 表达式,并对其执行 eval

  • 您在脚本中使用了某些 8 位字符,例如 。您可能需要使用 # -*-coding:latin-1 -*- 行(通常称为coding-cookie)在脚本顶部定义编码。

  • 此外,在进行 str 比较时,规范化字符串并进行比较。 (人们使用 lower() it)这有助于为用户输入提供很少的灵活性。

  • 我还认为阅读 Python 教程可能会对您有所帮助。 :)

示例代码

#-*- coding: latin1 -*-

while 1:
    date=raw_input("Example: March 21 | What is the date? ")
    if date.lower() == "march 21":

    ....

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 an eval on them.

  • You are using certain 8bit characters in your script like . 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

#-*- coding: latin1 -*-

while 1:
    date=raw_input("Example: March 21 | What is the date? ")
    if date.lower() == "march 21":

    ....
っ〆星空下的拥抱 2024-10-25 15:28:10

我遇到了这个错误,因为一行中缺少右括号。

我一开始就遇到了这样一行的问题:
语法无效(、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.

酒儿 2024-10-25 15:28:10

我使用以下代码来获得 Python 2 和 3 兼容性

if sys.version_info < (3, 0):
    input = raw_input

I'm using the follow code to get Python 2 and 3 compatibility

if sys.version_info < (3, 0):
    input = raw_input
等数载,海棠开 2024-10-25 15:28:10

虽然 @simon 的答案在 Python 2 中最有帮助,但 Python 3 中不存在 raw_input 。我建议执行以下操作以确保您的代码在 Python 2 和 Python 3 中同样有效:

首先, pip install future:

$ pip install future

第二:从 future.builtins 导入输入

# my_file.py    
from future.builtins import input
str_value = input('Type something in: ')

对于上面列出的具体示例:

# example.py
from future.builtins import input
my_date = input("Example: March 21 | What is the date? ")

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:

$ pip install future

Second: import input from future.builtins

# my_file.py    
from future.builtins import input
str_value = input('Type something in: ')

And for the specific example listed above:

# example.py
from future.builtins import input
my_date = input("Example: March 21 | What is the date? ")
泅人 2024-10-25 15:28:10

我试图一般性地回答,与这个问题无关,当您将语法分成两半并忘记另一半时,通常会发生此错误。就像我的例子一样:

try :
 ....

因为 python 正在搜索 a

except Exception as e:
 ....

但它遇到了 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:

try :
 ....

since python was searching for a

except Exception as e:
 ....

but it encountered an EOF (End Of File), hence the error. See if you can find any incomplete syntax in your code.

千秋岁 2024-10-25 15:28:10

我遇到了同样的事情,我弄清楚了问题是什么。当我们使用方法输入时,我们应该输入的响应应该用双引号引起来。就像你的行一样
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 this input method - but it works this way.

Hope it helps

耳根太软 2024-10-25 15:28:10

在第一个 if 语句之后,不要输入“if”,而是输入“elif”,然后它应该可以工作。

前任。

`    while 1:
    date=input("Example: March 21 | What is the date? ")
if date=="June 21":
    sd="23.5° North Latitude
elif date=="March 21" | date=="September 21":
    sd="0° Latitude"
elif date=="December 21":
    sd="23.5° South Latitude"
elif sd:
    print sd `

After the first if statement instead of typing "if" type "elif" and then it should work.

Ex.

`    while 1:
    date=input("Example: March 21 | What is the date? ")
if date=="June 21":
    sd="23.5° North Latitude
elif date=="March 21" | date=="September 21":
    sd="0° Latitude"
elif date=="December 21":
    sd="23.5° South Latitude"
elif sd:
    print sd `
相守太难 2024-10-25 15:28:10

您可以尝试使用普通的 input 命令像平常一样为 python 编写代码。然而,诀窍是在编程开始时添加命令input=raw_input

现在您所要做的就是禁用(或启用),具体取决于您是在 Python/IDLE 还是终端中运行。您只需在需要时添加“#”即可完成此操作。

关闭以在 Python/IDLE 中使用

    #input=raw_input 

,当然打开以在终端中使用。

    input=raw_input 

我不确定它是否总是有效,但它是简单程序或脚本的可能解决方案。

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 command input=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

    #input=raw_input 

And of course switched on for use in terminal.

    input=raw_input 

I'm not sure if it will always work, but its a possible solution for simple programs or scripts.

羁〃客ぐ 2024-10-25 15:28:10

检查您的编译器的版本。

  1. 如果您正在处理 Python2 则使用 -

n= raw_input("请输入您的输入:")

  1. 如果您正在处理 python3 使用 -

n= input("请输入您的输入:")

Check the version of your Compiler.

  1. if you are dealing with Python2 then use -

n= raw_input("Enter your Input: ")

  1. if you are dealing with python3 use -

n= input("Enter your Input: ")

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文