dimanche 6 décembre 2020

How can I create x amount of lists based on user input? [duplicate]

I have to write a program that creates a square matrix by user defined dimensions. So if a user enters "3" as the dimension, I want my program to display a 3 by 3 matrix. I have to fill in the matrix with random numbers between 1 and 15. So far I have this:

dimension = int(input("Enter the number of rows and columns: \n")) 
import random
for i in range(1,dimension):
    randomMatrix = random.sample(range(1,16), dimension)
print(randomMatrix)

However, it just displays one row of the matrix and not all that the user specified. I don't know how to white the loop so that it loops the random list to create a random matrix by the dimensions specified by the user. Any help would be greatly appreciated.




Aucun commentaire:

Enregistrer un commentaire