Python EOF error when reading input
Andrew Mclaughlin
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".
Can you please help? I am running Python 2.7.5
Error Traceback
Traceback (most recent call last):
File "prog.py", line 1, in <module>
EOFError: EOF when reading a line 5 4 Answers
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.
EOF error is expected if no data is given when calling input or raw_input as explained in the documentation.
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.
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.
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.
This:
for i in range(0,n+1):does n+1 number of iterations, yet your input file:
5
6
11 21
21 0
31 52
41 61
61 0Only has n number of lines remaining when that loop is about to start. Upon attempting to read a n+1th line, you'll get an EOFError as there are no more lines.
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.
I had to change these configurations by unchecking the "emulate terminal in output console" box in the picture below and then it worked perfectly.