1
I’m wondering how to create a function(imprime_matriz
) which receives a matrix as a parameter and prints the matrix line by line. Obs: Spaces after the last element of each line should not be printed. Someone can help me?
The code I tried was this:
def cria_matriz(num_linhas, num_colunas):
matriz = []
for i in range(num_linhas):
linha = []
for j in range(num_colunas):
valor = int(input("Digite o elemento [" + str(i) + "][" + str(j) + "]"))
linha.append(valor)
matriz.append(linha)
return matriz
def le_matriz():
lin = int(input("Digite o número de linhas da matriz: "))
col = int(input("Digite o número de colunas da matriz: "))
return cria_matriz(lin, col)
m = le_matriz()
Option 2: (shorter)
def imprime_matriz(matriz):
linhas = len(matriz)
colunas = len(matriz[0])
for i in range(linhas):
for j in range(colunas):
if(j == colunas - 1):
print("%d" %matriz[i][j], end = "")
else:
print("%d" %matriz[i][j], end = "")
print()
If you can give the dimension of the matrix would be better. Even if you can post the code you tried and the matrix you are using.
– Wilker
@Wilker put my code in the question.
– Monica da Silva Vasconcelos
I updated the answer.
– Wilker