Search code examples
python-3.xlistinsertindex-error

Using list.insert(i, x) for editing doubled lists


I came across a small problem while programming my TicTacToe game. It has to do with 2 lists being inside another list and the insert attribute.

game = [[' ',' '],[' ',' ']]
x,y = int(input(' ')).split(',')
del game[x-1][y-1]

And now i want to insert 'X' at game[x-1][y-1] i tried it with game.insert(i, x) but I cant use tuples with this method.

I would apreciate it if u could give me some tips, how to handle this problem.

V2

The problems seems to be fixed but another problem occurred... I’ll post the code here for better understanding.

print(' |',game[2][0],
       '|',game[2][1],
       '|',game[2][2],
       '|')

The error occurs in game this line of code. The error message says:

Trackback (most recent call last):
   File „python“, line 43, in <module>
   File „python“, line 14, in drawboard
IndexError: list index out of range

Some help again would be appreciated

And thanks Patrick Haugh and Rafael, They helped me at my last problem.

I solved all problems myself for now


Solution

  • You can use simple assigment instead of deleting

    game = [[' ',' '],[' ',' ']]
    x,y = int(input(' ').split(','))
    game[x-1][y-1] = 'X'