Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does standard input() cause an EOF error

I was solving a problem on HackerRank when I encountered the following problem in my code. I tested it out on my Python (2.7.10) IDLE , and it was working fine. But it showed the following error on HackerRank:

Traceback (most recent call last):
  File "solution.py", line 13, in <module>
    input_2=input()
EOFError: EOF when reading a line

I entered the following code:

import sys 
input_2=""
n=int(input())
m=0
l=0
array=[]
main_array=[]
for i in range (0,n):
    inp=input()
    array=(inp.split(" "))
    main_array+=array   
for no in range(0,100000,1):    
    input_2=input()
    for m in range(0,len(main_array),2):
        l=0
        if input_2==main_array[m]:
            l+=1
            print (main_array[m]+"="+main_array[m+1])
    if l==0:
        print ("Not found")

I don't know why this error turned up in the HackerRank Engine. Thanks

like image 637
Yajur Tayal Avatar asked Nov 27 '16 12:11

Yajur Tayal


People also ask

What causes EOF error?

Unexpected EOF implies that the interpreter has reached the end of our program before executing all the code. This error is likely to occur when: we fail to declare a statement for loop ( while / for ) we omit the closing parenthesis or curly bracket in a block of code.

How do I fix EOF error in Python?

How EOFError can be overcome? We overcome this problem by using keywords like try() and except() in Python. This is referred to as Exception Handling.

What does EOF mean in Python?

EOF stands for "end of file," and this syntax error occurs when Python detects an unfinished statement or block of code.

How do I use raw input in Python?

The raw_input() function reads a line from input (i.e. the user) and returns a string by stripping a trailing newline. This page shows some common and useful raw_input() examples for new users. Please note that raw_input() was renamed to input() in Python version 3.


1 Answers

I encountered similar EOF issues when I coded in HackerRank. Actually, there are 2 issues:

  1. use input() in Python3 and raw_input() in Python2.
  2. If you know the exact number of input, you can N-number for-loop to handle each input(). However, the difficulty is that you don't know how many inputs you will have. In this case, you will have to use sys.stdin.readlines() to convert them into a list, and then use a for-each loop.

The following codes are from "Editorial" of https://www.hackerrank.com/challenges/30-dictionaries-and-maps/

import sys 

# Read input and assemble Phone Book
n = int(input())
phoneBook = {}
for i in range(n):
    contact = input().split(' ')
    phoneBook[contact[0]] = contact[1]

# Process Queries
lines = sys.stdin.readlines()  # convert lines to list
for i in lines:
    name = i.strip()
    if name in phoneBook:
        print(name + '=' + str( phoneBook[name] ))
    else:
        print('Not found')
like image 64
Yuchao Jiang Avatar answered Oct 30 '22 03:10

Yuchao Jiang