简体   繁体   中英

Python Lo She Magic Square

First, I want to apologize if my format is incorrect. This is my first post on this site. I've looked through other posts about this but none seemed to match how my code needs to output. My code has to be more than words saying that a square is a lo she magic square. I have to display a 3x3 table with random numbers 1-9, and they have to equal 15 at all sides. Currently I'm trying to set up my table and relate it to the rest of my code which will determine what the square is or not. Currently I'm having a problem with setting the table up, and instead of it being a 3x3, its showing a 6x3. Again, I apologize for not formatting my post correctly.

def main(): #Parent Function

    import random
#****************************************************



    square_1 = random.randint(1, 9)
    square_2 = random.randint(1, 9) 
    square_3 = random.randint(1, 9)
    square_4 = random.randint(1, 9) 
    square_5 = random.randint(1, 9)
    square_6 = random.randint(1, 9) 
    square_7 = random.randint(1, 9)
    square_8 = random.randint(1, 9)
    square_9 = random.randint(1, 9)

    Grid_1 = [square_1 ,'\t', square_2 ,'\t', square_3]
    Grid_2 = [square_4 ,'\t', square_5 ,'\t', square_6]
    Grid_3 = [square_7 ,'\t', square_8 ,'\t', square_9]

    for i in range(1):
        print (','.join(str(i) for i in Grid_1))
    for i in range(2):
        print (','.join(str(i) for i in Grid_2))
    for i in range(3):
        print (','.join(str(i) for i in Grid_3))

main()

ANALYSIS :

Your code did exactly what you told it to do: print the first row once, the second row twice, and the third row 3 times. Also, note that you used the variable i as a loop index, but then mis used it again as an index within the print statement.

IMMEDIATE REPAIR :

Don't use those loops at all: print each row once:

Grid_1 = [square_1 ,'\t', square_2 ,'\t', square_3]
Grid_2 = [square_4 ,'\t', square_5 ,'\t', square_6]
Grid_3 = [square_7 ,'\t', square_8 ,'\t', square_9]

print (','.join(str(i) for i in Grid_1))
print (','.join(str(i) for i in Grid_2))
print (','.join(str(i) for i in Grid_3))

OVERALL REPAIR :

Develop your board as a 3x3 nested list (think of it as a 2-D matrix). See the answer Ajax1234 posted for a good way to handle that. With this organization, you can iterate (run a for loop) easily through either rows or columns, something like

for row in range(3):
    if sum([board[row][col] for col in range(3)]) != 15:
        ... board failed

ALGORITHM

You might note that the problem of detecting the square can take some short-cuts: there is only one Lo She magic square form; all of them are rotations/reflections of the original. You can make quick checks if you wish: 5 must be at the center, and the even numbers must be in the corners. All 9 numbers must appear. Once you've guaranteed those, there is a minimal amount of 15 checking you must do -- and I'll leave that algebra to the student.

An easy way is through list comprehension:

import random
the_board = [[random.randint(1, 9) for i in range(3)] for b in range(3)]

This now initializes your board as a 3x3 matrix with random values.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM