Function does not modify the Matrix after the second call!

Asked

Viewed 36 times

-1

I’m making a small game, however, I need to call this function 3 times, in the first call of the function it performs as expected, however, in the second and third call it does not do what I expect. That is, from the second call it does not modify the matrix ( it takes the original matrix and not the modified matrix after the first call ) which is what I need because that’s why I created the function. function does not modify the matrix outside the function? I can not put the matrix inside the function, pq it would be executed 3 times.

import random
import time
Tamanho = 21
Matriz = [[0]* 7 for i in range(3)]
Lista_De_Numeros = [Numero + 1 for Numero in range(Tamanho)]
for i in range(3):
    for j in range(7):
        Numero = random.choice(Lista_De_Numeros)
        Matriz[i][j] = Numero
        Lista_De_Numeros.remove(Numero)
def Escolha (M):
    Linha_Do_Numero = int(input("Qual a linha que seu número ficou ? "))
    if Linha_Do_Numero == 1:
        M = [(M[1]), (M[0]), (M[2])]
        print(M)
        M = [[(M[0][6]), (M[0][3]), (M[0][0]), (M[1][4]), (M[1][1]), (M[2][5]),(M[2][2]) ], [(M[0][5]), (M[0][2]), (M[1][6]), (M[1][3]), (M[1][0]), (M[2][4]), (M[2][1])], [(M[0][4]), (M[0][1]), (M[1][5]), (M[1][2]), (M[2][6]), (M[2][3]), (M[2][0])]]
        print(M)
    elif Linha_Do_Numero == 2:
        M= [(M[0]), (M[1]), (M[2])]
        print (M)
        M = [[(M[2][6]), (M[2][3]), (M[2][0]), (M[1][4]), (M[1][1]), (M[0][5]),(M[0][2]) ], [(M[2][5]), (M[2][2]), (M[1][6]), (M[1][3]), (M[1][0]), (M[0][4]), (M[0][1])], [(M[2][4]), (M[2][1]), (M[1][5]), (M[1][2]), (M[0][6]), (M[0][3]), (M[0][0])]]
        print (M)
    elif Linha_Do_Numero == 3:
        M= [(M[0]), (M[2]), (M[1])]
        print (M)
        M = [[(M[2][6]), (M[2][3]), (M[2][0]), (M[1][4]), (M[1][1]), (M[0][5]),(M[0][2]) ], [(M[2][5]), (M[2][2]), (M[1][6]), (M[1][3]), (M[1][0]), (M[0][4]), (M[0][1])], [(M[2][4]), (M[2][1]), (M[1][5]), (M[1][2]), (M[0][6]), (M[0][3]), (M[0][0])]]                   
        print (M)
print()
print("                              Escolha um único número abaixo")
print()
print('       linha 1','                        linha 2','                      linha 3')
print()
print (Matriz[0],'     ',Matriz[1],'     ',Matriz[2])
print()
Escolha(Matriz)
print()
Escolha(Matriz)
print()
Escolha(Matriz)
print()
print(Matriz[1][3])

1 answer

0


Python arrays are passed by reference, that is, the modifications made in a function are passed back to the matrix definition we made outside the scope of the function. An example:

matriz = [[0]*3 for i in range(3)]

print("matriz original " , matriz)

def escolha(m):
    for i in range(3):
        for j in range(3):
            'alguma operacao na matriz'
            m[i][j] = m[i][j] + 1
    return m

escolha(matriz)

print("funcao um modifica a matriz original", matriz)

escolha(matriz)

print("funcao dois também modifica a matriz original", matriz)

What returns:

matriz original  [[0, 0, 0], [0, 0, 0], [0, 0, 0]]                                                                                            
funcao um modifica a matriz original [[1, 1, 1], [1, 1, 1], [1, 1, 1]]                                                                        
funcao dois também modifica a matriz original [[2, 2, 2], [2, 2, 2], [2, 2, 2]]   
  • I didn’t understand, it was a bit superficial the explanation, anyway thank you

  • now it was well explained, but even with the Returns my function continues to do the same thing!

Browser other questions tagged

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