I can not understand the cause of this error [closed]

-4

I'm doing a project in python's game four online and I have the value function that tells me what value is placed at a certain point in 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 graphical mode that will use the pygame to create the game graph and use the value function to get a given yellow table or yellow position in a given table position.

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 code gives me the following error and I can not figure out why. Thank you for your attention.

    
asked by anonymous 13.12.2018 / 18:15

1 answer

1

Your error:

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

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

This only leaves two possibilities: either grelha is int (and you're trying to access [linha] in it) or grelha[linha] is int (and you're trying to access% / p>

As defined in the piece of code you pasted, [coluna] is grelha :

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, you should not make that mistake. I can not play 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 that you omitted from the question, you have some redefinition or modification of any of these variables, which would be putting / passing an object of type jogo[0] instead of the list.

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

    
13.12.2018 / 20:57