lundi 23 mars 2020

I am trying to create a list of 9 randomly generated numbers, all of which are different

I am trying to create a list of 9 randomly generated numbers, all of which are different numbers. The end goal is to create a magic square (sums of 15). I am trying to confirm that my list mat1 correctly generated the 9 random numbers, but my output is nothing.

import random
while True: # keep trying until you find a magic square 
    magic=True # assume that magic is True, as a start
    mat1=[] # intialize matrix1 (mat1) to hold 9 unique numbers
    i=1
    while i < 10:
        val=random.randint(1,9) # generate a random number between 1 and 9
        if val not in mat1: # check if the generated number exists in the list
            mat1.append(val) # if it doesn't exist in the list then append it to the list
    print(mat1)



Aucun commentaire:

Enregistrer un commentaire