Z
Z
Zakhar Kravchenko2020-02-26 15:52:36
Python
Zakhar Kravchenko, 2020-02-26 15:52:36

How to make a 2D array with manual input?

I recently started learning Python and ran into a problem - I need to write a program that takes a rectangular matrix as a sequence of lines as input, ending with a line containing only the string "end".
Tried to implement something, in the end nothing comes out. I do not understand how to make an empty two-dimensional list filled line by line.

list = 
cnt = 0
while 0 == 0:
    cw = [int(i) for i in input().split()]
    if 'end' in cw:
        break
    list[cnt] = cw
    cnt += 1
for i in range(len(list)):
    for j in range(len(list[i])):
        print(j, end=' ')
    print()

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander, 2020-02-26
@LCarroll

According to your style:

L = []
while True:
    S = [i for i in input().split()]
    if 'end' in S:
        break
    N = [int(i) for i in S]
    L.append(N)
print(L)

spoiler
1 2 3 4 5
5 4 3 2 1
1 2 3 4 5
end

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question