Search code examples
pythondebuggingtypeerrortic-tac-toe

How do I get rid of this TypeError, and why is it occurring given that I am simply reassigning a value in a list?


Code:

http://pastie.org/1961455

Trackback:

Traceback (most recent call last):
  File "C:\Users\COMPAQ\Desktop\NoughtsCrosses.py", line 149, in <module>
    main ()
  File "C:\Users\COMPAQ\Desktop\NoughtsCrosses.py", line 144, in main
    move = computer_move(computer, board, human)
  File "C:\Users\COMPAQ\Desktop\NoughtsCrosses.py", line 117, in computer_move
    board[i] = computer
TypeError: 'str' object does not support item assignment

As you can see for in my tic-tac-toe program, the board[i] = computer line in the computer_move function is the one (if I am reading this right) causing the error. But if I know this right, item assignment is allowed in lists, and I create a local copy of "board" for my function so that I can reassign values and whatnot within the function...

Any input at all would be greatly appreciated. This is my first serious piece of code, so if the function in question looks too mangled


Solution

  • The problem is here:

    def computer_move (computer, board, human):
        best = (4,0,8,2,6,1,3,5,7)
        board = board [:]
        for i in legal_moves(board):
            board[i] = computer
            if winner(board) == computer:
                return i
            board = EMPTY
    

    At the end of the function, you assign EMPTY to board, but EMPTY is an empty string, as defined on line 4. I assume you must have meant board[i] = EMPTY.