"TypeError: object" int "does not support element assignment"; iterative problem

def create_board(): b = [[['',''] for i in range(8)] for j in range(8)] return b game_board = create_board() for i in game_board[0]: for idx, val in enumerate(i[1::2]): idx[0] = 0 idx[1] = 0 print game_board 

I have this script in which I need to iterate over the first list, which is in the game_board list. Starting from the second item, I need to change the values ​​in each list of other items. However, when I run this, I am greeted by an error

 idx[0] = 0 TypeError: 'int' object does not support item assignment 

It would be understandable if IDLE complained that I was assigning the str variable (which would be a problem with iterating over the values, not the indexes), but I don't understand why this problem is happening, given that I don't have integers.

+4
source share
2 answers

idx is just an integer like 0 , and there is no such thing a 0[0]

You want to use val, which is your item from your list.

it actually looks like you have other problems ...

fixed

 for row in game_board: for item in row: item[0] = 0 item[1] = 0 
+5
source

The enumerate () function returns a tuple that is (integer, object) - see the python documentation for an listing .

You are trying to index an integer that you cannot.

+1
source

Source: https://habr.com/ru/post/1481492/


All Articles