I'm new to coding. I'm trying to program a magic square. A magic square is a square (3 × 3 for my case, could be different), where all rows and columns and diagonals must sum to a certain number (15 for my case, because of the 3 × 3). Here is my code:
s = []
while len(s) < 9:
n = 0
a = random.randrange(1, 10)
while a not in s:
s.append(a)
while s[0] + s[1] + s[2] != 15 and s[3] + s[4] + s[5] != 15 and \
s[6] + s[7] + s[8] != 15 and s[0] + s[4] + s[8] != 15 \
and s[2] + s[4] + s[6] != 15 and s[0] + s[3] + s[6] != 15 and \
s[1] + s[4] + s[7] != 15 and s[2] + s[5] + s[8] != 15:
shuffle(s)
print(s)
I don't understand why the program isn't shuffling until all the criteria are met in the while loop. I know this is not the way to code this program and even if it would work, it would be randomness and brute forcing the solution, i would just like to understand what is happening inside the while loop.