I wanna learn how to make my code more Pythonic, I'm still a newb. Advises on classes and functions I could've used are welcomed.
import random as r
n = 7 #This number defines the size of the square matrix
def print_mat(mat):
""" This function will be used to display the matrix
"""
for row in range(len(mat)):
for line in range(len(mat[0])):
print(mat[row][line],end="\t")
print("\n")
def is_used(test_num):
""" This function tests if the argument has already been used
in the column of the matrix thanks to the array already_used
"""
for used_num in already_used:
if test_num == used_num:
return True
return False
A = [[0]*n for _ in range(n)] #Creates matrix of size n*n
for row in range(n):
""" Will loop n times for each column of the matrix
The array bellow will allow to sort out already used random numbers
"""
already_used = [0 for _ in range(n)]
for line in range(n):
""" Will loop n times for each line of the matrix
"""
rand_num = r.randint(1,n) #Generate random number between [1;n]
while is_used(rand_num) == True:
rand_num = r.randint(1,n)
""" Will loop until rand_num is a num that hasn't been used
"""
A[line][row] = rand_num #Assignes the available rand_num to the matrix
already_used[line] = rand_num
print("\nMatrice A = ")
print_mat(A) #Prints the matrix using the function defined above
One previous output :
Matrice A =
6 4 7 4 6 5 6
3 1 6 5 1 4 2
4 3 2 3 2 1 4
2 2 1 7 3 6 5
1 5 4 2 4 7 1
5 6 5 1 5 2 3
7 7 3 6 7 3 7
Thanks a lot <3