Search code examples
pythonarraysinputinitializationraw-input

How to initialize a 2D integer array using raw_input() in PYTHON


I need to initialize a 2D integer array[10*10] taking input from user in PYTHON. What is the code for this? I have tried doing this but it shows error as list index out of range

board = [[]]
for i in range(0,10):
    for j in range(0,10):
        board[i].append(raw_input())

Traceback (most recent call last): File "solution.py", line 162, in board[i].append(raw_input()) IndexError: list index out of range


Solution

  • board = []
    for i in range(10):
        row = []
        for j in range(10):
            row.append(j)
            # row.append(raw_input())
        board.append(row)
    
    >>> board
    [[0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]]
    >>> 
    

    For test purposes I inserted the counter instead of the raw_input value