Taking input Python

Hello, I am a rookie programmer and only recently started using CodeChef. I was testing out the CodeChef ide and realised that I got an error everytime i tried to take input on the Python ide. The error triggered for both the input() and raw_input() functions.If someone could suggest a fix or an alternative method of taking input, I’d appreciate it.
My code:

x = input()

Error:
Traceback (most recent call last):
File “./prog.py”, line 2, in
EOFError: EOF when reading a line

16 Likes

try to provide some input in custom input and then run

13 Likes

Yeah you had not given any custom data.

3 Likes

EOF error results usually in an online judge like CodeChef when an input is given by the system but the input reading line is not at the right place to read it.
Read the sample input syntax again and try.

3 Likes

Its happen because when the compiler compile the code it get EOF at end and nothing else to do.
Try using try-except method for this

while True:
    try:
        a = input()
    except EOFError:
        print ("EOFError")
        break

and you can provide some custom input also.

5 Likes

Give the input by your own in custom input

when i give the data in custom input , it execute the code without any error and give the right result , but on submitting the same code it gives the same error(runtime error
NZEC)

22 Likes

Try this trick:
try:
//-----Your Code-----
//----Your Code-----
//—Your Code-------
except:
pass

47 Likes

did you get any solution? i have similar problem

7 Likes

Same here. The code is working fine with custom input. But submission fails. @imsankeerth: How did you take input in CHFM problem?

6 Likes

If u write a recursive solution with depth greater than 1000, then it can also cause nzec.

1 Like

I just started codechef and I’m too getting that EOF error each time when the code is allowed to get compiled with system’s input.
For some programs i could find solution when custom input is provided.
and also you can better try :
try … except …

2 Likes

Even i’m having the same problem!

First of all, don’t use codechef IDE. It’s very slow and most of the time it sucks. Even though you are using it, provide custom input test cases.

You can also use ‘try and except’ in your code.
try:
----your code----
----your code----
----your code----
except EOFError as e : pass

An EOFError is raised when a built-in function like input() or raw_input() do not read any data before encountering the end of their input stream.

For python refer to: Python input/output for competitive programming.

For matrix input in python: matrix = [[ int ( input ()) for x in range (C)] for y in range (R)] , where R is no. of Rows and C is no. of Columns.

4 Likes

You can use readline() from sys package. Refer the following lines of code:
import sys
t = int ( sys.stdin.readline() )
print(t)

2 Likes

https://www.codechef.com/viewsolution/25360383

^^ That’s my take on the BST operations challenge. When I run the code (without custom input), i get an empty input stream which returns “invalid literal for base 10”. But with custom input it runs perfectly. The same thing happens when I submit it. Is there something wrong with my code?

1 Like

thanks dude it worked

1 Like

try this
x = input().split(’ ‘)
then x will be a list with every element as string
so you need to convert each element into int by type casting
example:
code:-
x = input().split(’ ')
print(x)

input:-
1 2 3
o/p:-
[‘1’, ‘2’, ‘3’]

3 Likes

Thanks it really works

its working…bt it increased my code execution time by 5sec nd now it shows TLE(Time Limit exceeded) error.
Wanted to know what to do.
Is there any other way to handle this exception?

3 Likes