2
I need to create a matrix and fill it with random values (randint(0,9)
), except in equal rows and columns. For example, in this way:
A| B| C| D| E
A x| 2| 1| 4| 2
B 1| x| 1| 1| 2
C 5| 1| x| 4| 3
D 8| 2| 1| x| 2
E 5| 4| 1| 4| x
I’ve done this before:
from random import randint
def matrix():
n=5
m=10
matrixC = []
value = (randint(0,9))
#adicionar esse valores à matriz gerada
for i in range(0, n):
for j in range(0, n):
if i != j:
matrizC.append[[random.random() for i in range(n)] for j in range(n)]
else:
matrizC.append("x")
[UPDATE]
Following the directions I have this:
from random import randint
def matriz():
#pedir o n
n=5
matriz = [] #declaração da matriz
n_col=1
n_lin=1
for n_lin in range(n):
linha = [] # cria uma linha para a matriz
for n_col in range(n):
if n_lin==n_col:
linha.append('x')
else:
linha.append(randint(1,9))
matriz.append(linha)
n_col=1
n_lin=1
for n_lin in range(n):
for n_col in range(n):
print(matriz[n_lin][n_col], end='\t')
In the 3rd go I print the matrix, but when compiling I get no results.
There doesn’t seem to be anything wrong with the question - there is the initial code of the A.P., which has some parts missing, and an answer may guide it in the right direction.
– jsbueno
Elaine, would [Dit] be your post adding a brief explanation of what went wrong or what did not correspond to the expectation in the code you tried? The post started well, with relevant details, but lacked a description of where exactly is the problem, which is precisely the doubt of programming in site scope. It ended up being a "question without question", but I believe that a simple edition complementing solve.
– Bacco
That’s all your code? you know that in Python there is no special named function that is called automatically - rather that the program runs from start to finish, and functions need to be called. To see any result - even if it is not what you expect there, you have to call the function `matrix() in the module body.
– jsbueno