Python - Tabuada- For Exercise - Matrix

Asked

Viewed 652 times

1

Good night! I’m trying to create a tabuada program using for and matrix, my code is like this at the moment:

def crie_array(rows, columns, value):

    matriz = [] 
    for i in range(linhas):
        linha = []

        for j in range(colunas):
            valor = i*j
            linha.append(valor)
        matriz.append(linha),"\n"

    return matriz

A = crie_matrix(11,11,11) print(A)

It works the problem is that in the Console I’m only getting the results of multiplication, when in fact what I want is to receive:

[1 x 1 = 1] [1 x 2 = 2] [1 x 3 = 3] [1 x 4 = 4] [1 x 5 = 5] [1 x 6 = 6] [1 x 7 = 7] [1 x 8 = 8] [1 x 9 = 9] [1 x 10 = 10]

This is what I want to receive, but not only from the table of 1, but from the table of 1. Someone can help me.

1 answer

2

When filling the matrix, you can put in the list linha not only the value, but the information you want:

linha.append('{} x {} = {}'.format(i, j, valor))

Take a look to learn more about string formatting.

  • Thank you so much for the help, it worked super well! Vlw my friend.

Browser other questions tagged

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