0

I made a function to create matrices for a board game. The function receives as a parameter the number of boards and the "difficulty" (if the matrices are 3x3, 4x4 or 5x5). The function normally works in situations involving 1 board, but when I try to generate two simultaneously I can only go up to 3x3, otherwise I get "ValueError: Sample larger than population or is negative". If anyone can help me understand what's going on I will be eternally grateful =)

def createMatrice(boardNumbers, dificulty):
    numList = []
    
    for i in range(1, 30+ 1):
        numList.append(i)
    random.shuffle(numList)
    copyofNumList = numList
    
    matrice = []
    secondMatrice = []

    for i in range(dificulty):
            numbers = random.sample(numList, dificulty)
            for j in numbers:
                numList.pop(numList.index(j))
            matrice.append(numbers)

    if boardNumbers == 2:
        for i in range(dificulty):
            numbers = random.sample(copyofNumList, dificulty)
            for j in numbers:
                copyofNumList.pop(copyofNumList.index(j))
            secondMatrice.append(numbers)
    
    return matrice, secondMatrice
Barmar
  • 669,327
  • 51
  • 454
  • 560
  • `copyofNumList = numList` doesn't make a copy of `numList`. Both variables refer to the same list. – Barmar May 01 '22 at 22:42

0 Answers0