0

I am new to python and had a problem, wherein input from the online judge is supposed to be in the form] of a horizontal row of integers. I am trying to store them as a list. input format: 5 4 4 2 2 8 But every time I type in horizontal row of characters I get the error : unexpected EOF while parsing

Is there a way to do it in Python like there is Scanner class in python that easily inputs various data type.

Edit: It will be really helpful if you can tell how do we differentiate which data type is input by the user for the input above can easily considered a String.

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153

1 Answers1

0

A convenient way to read input from stdin when using online judges is to iterate through the input lines like this:

for line in sys.stdin:
    inp_array = line.split() # split the line
    a = int(inp-array[0])
    b = float(inp_array[1])

This would result in that the input 3 2.5 is read into the variables a and b

When getting user input through stdin the best way is to use the input() method. This returns a string that you then can try to convert to whatever datatype you want (and handle the error if what the user provided isn't of the type you expected)