Python 3: Eof When Reading a Line (Sublime Text 2 Is Angry)

Python 3.1 and Sublime Text 2 error

Install sublimeREPL and then choose Tools->sublimerepl->python.

Reference:http://gimo.me/sublime-text2-skills/

EOFError: EOF when reading a line

width, height = map(int, input().split())
def rectanglePerimeter(width, height):
return ((width + height)*2)
print(rectanglePerimeter(width, height))

Running it like this produces:

% echo "1 2" | test.py
6

I suspect IDLE is simply passing a single string to your script. The first input() is slurping the entire string. Notice what happens if you put some print statements in after the calls to input():

width = input()
print(width)
height = input()
print(height)

Running echo "1 2" | test.py produces

1 2
Traceback (most recent call last):
File "/home/unutbu/pybin/test.py", line 5, in <module>
height = input()
EOFError: EOF when reading a line

Notice the first print statement prints the entire string '1 2'. The second call to input() raises the EOFError (end-of-file error).

So a simple pipe such as the one I used only allows you to pass one string. Thus you can only call input() once. You must then process this string, split it on whitespace, and convert the string fragments to ints yourself. That is what

width, height = map(int, input().split())

does.

Note, there are other ways to pass input to your program. If you had run test.py in a terminal, then you could have typed 1 and 2 separately with no problem. Or, you could have written a program with pexpect to simulate a terminal, passing 1 and 2 programmatically. Or, you could use argparse to pass arguments on the command line, allowing you to call your program with

test.py 1 2

Python 3: EOF when reading a line (Visual Studio Code)

I reproduced the problem you described:

Sample Image

Reason: When we use "console": "internalConsole", the result will be output to "DEBUG CONSOLE", and this terminal of VSCode is currently only used for display output. When the code needs to be input but input is not received, it will throw "EOF" "(End of file), "There is an unexpected error at the end of the file".

Solution: please change the output mode of the debugging code. (in settings.json file)

  1. For the code that needs to be entered, we can use "console": "integratedTerminal",

    Sample Image

  2. or use "console": "externalTerminal",

    Sample Image

Reference: console in VSCode.

Why does standard input() cause an EOF error

I don't know what are you exactly trying to achieve through your code, but you are getting an error due to the line inp=input(), which tries to take a string as input, whereas it's designed to take an int, float, or string entered with quotes in python 2.7. Use raw_input() instead to take the complete line as string input, and then split it with space as delimiter.

I followed the link you provided, and I see that the given question needs to be solved using dictionaries. Hence, instead of appending the name and number, just store it in dictionary as a key-value pair and for each query, just check whether the key exists in the dictionary or not. If not, print Not found, else print the key and its corresponding value. Here is the code for reference :

import sys
input_2=""
n = input()
m=0
l=0
array=[]
main_array={}
for i in range (0,n):
inp=raw_input()
array=(inp.split())
main_array[array[0]] = array[1]
for i in range(0,n):
take = raw_input()
if take in main_array :
print take+"="+main_array[take]
else :
print "Not found"

Hope this helps !

EOFError: EOF when reading a line, can't figure out why

I think you are using an online IDE, or giving an empty input file to read input from.

I was able to re-generate your exception, when i created an empty file empty.txt and passed that as argument while running above code segment.

$ python3 main.py < empty.txt
Python Among Us

You are a Crewmate !
Where would you like to go? Traceback (most recent call last):
File "/Users/vishvanath/Desktop/test/main.py", line 8, in <module>
firstdestination = input("Where would you like to go? ")
EOFError: EOF when reading a line

Here, main.py contains code segment provided in question and empty.txt is just an empty file.

So error occurs, because your program is expecting an input, but there is none.



Related Topics



Leave a reply



Submit