0

So I want the user to enter a matrix in the form, for example:

 2 2
 3 4

I need Python to ignore the enter key here and treat it the same as the space bar, otherwise it would give me an error when I press the enter key after last number in the first row, making it only possible to enter the matrix as:

2 2 3 4

Is it possible?

kaya3
  • 47,440
  • 4
  • 68
  • 97
soup
  • 79
  • 5

1 Answers1

1

If you want the user to input multiple lines, you can't just ignore a newline; instead you'll have to take multiple lines of input. See How do I read multiple lines of raw input in Python?

For example, based on jamylak's answer:

matrix = []
for line in iter(input, ''):
    matrix.append([int(x) for x in line.split()])

for x in matrix:
    print(x)

Input (note the blank line at the end):

2 3
3 4

Output:

[2, 3]
[3, 4]
wjandrea
  • 28,235
  • 9
  • 60
  • 81