why isn't input() behaving right?

I’ve the following python 2.7.3 code which I am submitting to codechef online programming contest:

case = input() 
for i in xrange(0, case):
    try:
        l = [elem for elem in raw_input().split()]
        res = int(l[0][::-1]) + int(l[1][::-1])
        print int(str(res)[::-1])
    except:
        break

This works on my computer, even when I use input redirection and use a in.txt file for input, still it works.

But the problem is when i submit it for evaluation, I get an exception(NZEC) and that exception gets removed when I use raw_input for getting the value of case

case = int(raw_input())

My in.txt file is as follows:

1
23 45

My problem is that it’s working on my computer perfectly, what is it that the online contest site feeding at the 1st line that an exception is being raised, and further it gets rectified when I use raw_input.

Shouldn’t input() also work when my 1st line is always an integer?

Shouldn’t input() also work when my
1st line is always an integer?

That’s the point. Maybe the input file is not formatted as expected.

Remember input() is equivalent to eval(raw_input()), not int(raw_input()).
So if your user input isn’t a valid Python-syntax entry, it will raise an exception.
As a matter of fact, you should always use raw_input() instead of input() on codechef.

Good luck :slight_smile:

I would probably say stdin.readline() would be better. I use it all the time now. Changing from raw_input to readline in the Enormous Input Test was the difference between pass and fail. Though i’m not sure on the specifics of why that is. Just an observation.

Edit: To expand on that, I was failing due to timeout.

that’s because raw_input() is a high-level function, used to handle an optional prompt string, instead of stdin.readline(), which is an almost-direct mapping to the read system call on stdin file descriptor.

//