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

1 Like

try to provide some input in custom input and then run

4 Likes

Yeah you had not given any custom data.

2 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.

1 Like

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.

1 Like

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)

3 Likes

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

6 Likes

did you get any solution? i have similar problem

1 Like

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

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

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 …

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.

1 Like

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

1 Like

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?

thanks dude it worked

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’]

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?