pythonwhile-loopmagic-square

Magic square in python


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.


Solution

  • I think you've written the condition of your loop incorrectly. It currently requires that none of the rows, columns or diagonals add up to the right value. If any of them do, it quits, since the chained ands result in a False value.

    Instead, I think you want to use the or operator instead of the and operator. That way you'd keep looping as long as any of the conditions was true (meaning any of the lines didn't add up correctly).

    Or alternatively, you could keep the and operators, but change the != operators to == and negate the whole thing at the end (since not X or not Y is logically equivalent to not (X and Y)):

    while not (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):