why this python code is producing Runtime Error in ideone?

1.9k Views Asked by At
import sys
def func():
    T = int(next(sys.stdin))
    for i in range(0,T):
            N = int(next(sys.stdin))
            print (N)

func()

Here I am taking input T for for loop and iterating over T it gives Runtime error time: 0.1 memory: 10088 signal:-1 again-again . I have tried using sys.stdin.readline() it also giving same error .

1

There are 1 best solutions below

0
On

I looked at your code at http://ideone.com/8U5zTQ . at the code itself looks fine, but your input can't be processed.

Because it is:

5 24 2

which will be the string:

"5 24 2"

this is not nearly an int, even if you try to cast it. So you could transform it to the a list with:

inputlist = next(sys.stdin[:-2]).split(" ")

to get the integers in a list that you are putting in one line. The loop over that.

After that the code would still be in loop because it want 2 integers more but at least you get some output.

Since I am not totally shure what you try to achieve, you could now iterate over that list and print your inputs:

inputlist = next(sys.stdin[:-2]).split(" ")
for i in inputlist
    print(i)

Another solution would be, you just put one number per line in, that would work also

so instead of

5 24 2

you put in

5
24
2

Further Advice

on Ideone you also have an Error Traceback at the bottom auf the page:

Traceback (most recent call last):
  File "./prog.py", line 8, in <module>
  File "./prog.py", line 3, in func
ValueError: invalid literal for int() with base 10: '1 5 24 2\n'

which showed you that it can't handle your input