读取输入时出现Python EOF错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35668520/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python EOF error when reading input
提问by Mark
n = input()
dum = input()
d = {}
for i in range(0,n+1):
x = raw_input()
x = x.split(" ")
d[int(x[0])] = int(x[1])
array = d.keys()
for key in d.keys():
if(d[key]!=0):
if(d[key] not in d.keys()):
for i in d.keys():
for j in d.keys():
if(i!=j and i!=key and j!=key):
if(i+j==d[key]):
# print str(i)+"-"+str(j)
if(i in array):
array.remove(i)
if(j in array):
# print j
array.remove(j)
else:
# print d[key]
array.remove(d[key])
print array[0]
When I execute this Python code I am getting "EOF error when reading input".
当我执行此 Python 代码时,出现“读取输入时出现 EOF 错误”。
Can you please help? I am running Python 2.7.5
你能帮忙吗?我正在运行 Python 2.7.5
Error Traceback
错误追溯
Traceback (most recent call last):
File "prog.py", line 1, in <module>
EOFError: EOF when reading a line
回答by Forge
I can't seem to reproduce this error although using the same input as you did. Maybe you have a newline
character before the input you have specified?
Try running this code using python prog.py
in your terminal.
尽管使用与您相同的输入,但我似乎无法重现此错误。也许您newline
在指定的输入之前有一个字符?
尝试python prog.py
在终端中使用运行此代码。
EOF error
is expected if no data is given when calling input
or raw_input
as explained in the documentation.
EOF error
如果在调用时未提供数据input
或raw_input
如文档中所述,则为预期。
Also, it's advisable to use raw_input
and not input
when getting input from the user on python 2
, it's not going to fix your error though.
此外,建议使用raw_input
而不是input
在从用户获取输入时使用python 2
,但它不会修复您的错误。
回答by Hossain Muctadir
In Python 2, raw_input()
returns a string, and input()
tries to run the input as a Python expression. So, changing your first line to something like this should work.
在 Python 2 中,raw_input()
返回一个字符串,并input()
尝试将输入作为 Python 表达式运行。因此,将您的第一行更改为这样的内容应该可行。
n = int(raw_input())
According to the official documentation
根据官方文档
Equivalent to eval(raw_input(prompt)).
This function does not catch user errors. If the input is not syntactically valid, a SyntaxError will be raised. Other exceptions may be raised if there is an error during evaluation.
相当于 eval(raw_input(prompt))。
此函数不会捕获用户错误。如果输入在语法上无效,则会引发 SyntaxError。如果评估期间出现错误,则可能会引发其他异常。
回答by jonatan
This:
这个:
for i in range(0,n+1):
does n+1
number of iterations, yet your input file:
不n+1
重复的号码,但输入文件:
5
6
11 21
21 0
31 52
41 61
61 0
Only has n
number of lines remaining when that loop is about to start. Upon attempting to read a n+1
th line, you'll get an EOFError as there are no more lines.
n
当该循环即将开始时,只有剩余的行数。尝试读取n+1
第 th 行时,您将收到 EOFError,因为没有更多行了。
回答by Benjamin Lowry
I was really confused by this problem as well, and I couldn't find the direct answer on other resources.
我也被这个问题搞糊涂了,我在其他资源上找不到直接的答案。
However, what the issue was for me was I am using PyCharm and my configurations for my .py file were wrong. Basically, I had it looking for an external file to source input from instead of the console.
但是,对我来说问题是我正在使用 PyCharm 并且我的 .py 文件配置错误。基本上,我让它寻找一个外部文件来获取输入而不是控制台。
I had to change these configurations by unchecking the "emulate terminal in output console"box in the picture below and then it worked perfectly.
我不得不通过取消选中下图中的“在输出控制台中模拟终端”框来更改这些配置,然后它就完美地工作了。