I can’t figure out what’s causing this mistake

Asked

Viewed 51 times

-4

I’m doing a python project of game four online and have the value function that tells me what value is placed at a certain point of the table.

grelha = [[0, 0, 0, 0, 0, 0, 0],
      [0, 0, 0, 0, 0, 0, 0],
      [0, 0, 0, 0, 0, 0, 0],
      [0, 0, 0, 0, 0, 0, 0],
      [0, 0, 0, 0, 0, 0, 0],
      [0, 0, 0, 0, 0, 0, 0]]
fim = False
vencedor = None
jogador = 1
linha_vitoria = None
jogo = (grelha, fim, vencedor, jogador, linha_vitoria)


def valor(jogo, linha, coluna):
'valor'
##    grelha = jogo[0]
##    jogador_1 = 1
##    jogador_2 = 2
##    valor = 0
##
##    if grelha[linha][coluna] == 1:
##        valor = 1
##    elif grelha[linha][coluna] == jogador_2:
##        valor = 2
##    else:
##        valor = 0
##
##    return valor

    linha = linha - 1
    coluna = coluna - 1

    grelha = jogo[0]

    return grelha[linha][coluna]

Then I have the graphic mode that will use pygame to create the game chart and use the value function to get at a given table position which piece to put yellow or red.

def construir_frame_jogo():

    # a frame de jogo é sempre construída para o jogador humano. O
    # computador joga imediatamente a seguir ao jogador humano no
    # evento da jogada do jogador humano.

    global nova_frame

    # criar uma nova frame
    nova_frame = pygame.Surface(tamanho)

    # cor de fundo
    nova_frame.fill(cor_fundo)

    linha        = 1
    coluna_mouse = get_coluna_mouse()
    peca         = ordem_escolhida
    if ha_espaco(jogo, coluna_mouse):
        desenha_peca_jogo(linha, coluna_mouse, peca)

    for l in range(6):
        for c in range(7):

            peca = valor(jogo, l+1, c+1)
            desenha_peca_jogo(l+1+1, c+1, peca)

    # processar jogada
    if mouse_click == True:
        processar_jogada() 

But when I run the codeinserir a descrição da imagem aqui gives me the following mistake and I can’t figure out why. Thank you for your attention.

  • I couldn’t notice where you set the object jogo

  • I’ve played the game

1 answer

1

Your mistake:

 return grelha[linha][coluna]
 TypeError: 'int' object is not subscriptable

Indicates that this line is trying to access an element contained within an object int, as if it were a list. Since it is not a list, it returns this error.

That leaves only two possibilities: or grelha is int (and you’re trying to access the [linha] in it) or grelha[linha] is int (and you’re trying to access the [coluna] in it.

The way it was defined, on the piece of code you pasted, grelha is jogo[0]:

grelha = jogo[0]

And jogo[0] is the list of lists:

grelha = [[0, 0, 0, 0, 0, 0, 0],
      [0, 0, 0, 0, 0, 0, 0],
      [0, 0, 0, 0, 0, 0, 0],
      [0, 0, 0, 0, 0, 0, 0],
      [0, 0, 0, 0, 0, 0, 0],
      [0, 0, 0, 0, 0, 0, 0]]
jogo = (grelha, fim, vencedor, jogador, linha_vitoria)

So I shouldn’t make that mistake. I can’t reproduce it here. When trying to run a code similar to yours, completing what is missing, it works perfectly here.

The only explanation is that in the part of the code you omitted from the question, you have some redefinition or modification of any of these variables, which would be placing/passing an object of type int instead of the list.

In case you can’t find the problem with the explanation above, edit your question and add a MCVE; This will allow me to test here and find the problem.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.