For one of my assignments, rather than reading directly from a text file, we are directly taking the input from sys.in. I was wondering what the best way of obtaining this input and storing it would be?
So far, I've tried using:
sys.stdin.readlines() -- But this will not terminate unless it recieves an EOF statement, while a zero in my input signals the end of the file.
sys.stdin.readline() -- It will only read the final line of the input code.
input() -- I can only read in the first line of the input and when I try to loop it, I end up with a syntax error.
An example of the input is below:
3
2 1 3
3 2 1
1 3 2
2 1 3
3 2 1
1 3 2
2
1 2
1 2
2 1
2 1
0
My attempt at obtaining the input:
input_list = []
while True:
input_str = input("")
if input_str == '0':
break
else:
input_list.append(input_str)
print(input_list)
Returns this error while parsing the second line through:
Any help with this would be greatly appreciated. I've probably spent longer trying to get the input to work now than the actual assignment now.
EDIT: The version of Python we are using is 3.4
FINAL EDIT: Like both the answers said, it turns out that the university labs have a older version of python running when run in cmd prompt in the labs, where the input() method started behaving differently. Once I tried the code at home, it worked as intended.
