Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EOF Error in python Hackerrank

Trying to solve a problem but the compiler of Hackerrank keeps on throwing error EOFError while parsing: dont know where is m i wrong.

#!usr/bin/python

b=[]
b=raw_input().split()
c=[]
d=[]
a=raw_input()
c=a.split()
f=b[1]
l=int(b[1])
if(len(c)==int(b[0])):          
    for i in range(l,len(c)):
        d.append(c[i])
        #print c[i]
    for i in range(int(f)):
        d.append(c[i])
        #print c[i]
for j in range(len(d)):
    print d[j],

i also tried try catch to solve it but then getting no input.

try:
    a=input()
    c=a.split()
except(EOFError):
    a=""

input format is 2 spaced integers at beginning and then the array

the traceback error is:

Traceback (most recent call last):
  File "solution.py", line 4, in <module>
    b=raw_input().split()
EOFError: EOF when reading a line
like image 568
imshashi17 Avatar asked Mar 28 '17 18:03

imshashi17


People also ask

How do I fix EOF error in Python?

BaseException -> Exception -> EOFError The best practice to avoid EOF in python while coding on any platform is to catch the exception, and we don't need to perform any action so, we just pass the exception using the keyword “pass” in the “except” block.

What is an EOF error in Python?

EOF stands for End of File in Python. 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 overcome EOF error?

Explanation: In the above program, try and except blocks are used to avoid the EOFError exception by using an empty string that will not print the End Of File error message and rather print the custom message provided by is which is shown in the program and the same is printed in the output as well.

How do you fix EOFError EOF when reading a line in Python?

Read file. The most common reason for this is that you have reached the end of the file without reading all of the data. To fix this, make sure that you read all of the data in the file before trying to access its contents. You can do this by using a loop to read through the file's contents.


Video Answer


3 Answers

There are several ways to handle the EOF error.

1.throw an exception:

while True:
  try:
    value = raw_input()
    do_stuff(value) # next line was found 
  except (EOFError):
    break #end of file reached

2.check input content:

while True:
  value = raw_input()
  if (value != ""):
    do_stuff(value) # next line was found 
  else:
    break 

3. use sys.stdin.readlines() to convert them into a list, and then use a for-each loop. More detailed explanation is Why does standard input() cause an EOF error

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 92
Yuchao Jiang Avatar answered Oct 18 '22 03:10

Yuchao Jiang


I faced the same issue. This is what I noticed. I haven't seen your "main" function but Hackerrank already reads in all the data for us. We do not have to read in anything. For example this is a function def doSomething(a, b):a and b whether its an array or just integer will be read in for us. We just have to focus on our main code without worrying about reading. Also at the end make sure your function return() something, otherwise you will get another error. Hackerrank takes care of printing the final output too. Their code samples and FAQs are a bit misleading. This was my observation according to my test. Your test could be different.

like image 23
Digvijay Sawant Avatar answered Oct 18 '22 05:10

Digvijay Sawant


It's because your function is expecting an Input, but it was not provided. Provide a custom input and try to compile it. It should work.

like image 1
Tharun Kumar Avatar answered Oct 18 '22 05:10

Tharun Kumar