简体   繁体   中英

Lo Shu Magic Square - Keeps showing 'false' is not defined and then Invalid

I want my code to show that all rows, columns, and diagonals equal 15. And the numbers 1-9 can only show once in the 3 x 3 grid.

My code keeps telling me that false is not defined so when I change the false to "False" with a capital F all my code turns up as invalid instead of valid. The first 3 rows of my input file should show up as valid so I'm a bit confused.

input file:

4 3 8 9 5 1 2 7 6
8 3 4 1 5 9 6 7 2
6 1 8 7 5 3 2 9 4
6 9 8 7 5 3 2 1 4
6 1 8 7 5 3 2 1 4
6 1 3 2 9 4 8 7 5
5 5 5 5 5 5 5 5 5

my code:

def contains(grid, i):
#Returns true if i appears in the grid & returns false otherwise
    for row in grid:
        for value in row:
            if value == i:
                return true
    return False


def isValidMagicSquare(grid):
#make sure each # appears exactly once in grid
    for i in range(1, 10):
        if not contains(grid, i):
          return False
#make sure rows add to 15
    for row in grid:
        r1 = sum(grid[0])
        r2 = sum(grid[1])
        r3 = sum(grid[2])
        if r1 == 15 and r2 == 15 and r3 == 15:
            return true
        else:
            return false
#Make sure columns equal 15
    for column in grid:
        c1 = grid[0][0]+ grid[1][0]+ grid[2][0]
        c2 = grid[0][1]+ grid[1][1]+ grid[2][1]
        c3 = grid[0][2]+ grid[1][2]+ grid[2][2]
        if c1 == 15 and c2 == 15 and c3 == 15:
            return true
        else:
            return false
#Make sure diagonals equal 15
    for diagonal in grid:
        dL = full[0][0]+ full[1][1]+ full[2][2]
        dR = full[0][2]+ full[1][1]+ full[2][0]
        if dL == 15 and dR == 15:
            return true
        else:
            return false

def main():
    lst = []
    f = open('input.txt')
    for line in f:
        grid = [0, 0, 0]
        grid[0] = lst[0:3]
        grid[1] = lst[3:6]
        grid[2] = lst[6:9]
        if isValidMagicSquare(grid):
            print("Valid")
        else:
            print("Invalid")

main()

Its supposed to show:

valid
valid
valid
invalid
invalid
invalid
invalid
def main():
    lst = []
    f = open('input.txt')
    for line in f:
        grid = [0, 0, 0]
        grid[0] = lst[0:3]
        grid[1] = lst[3:6]
        grid[2] = lst[6:9]

In this code, lst is always an empty list. Therefore lst[0:3] , lst[3:6] , and lst[6:9] will also be empty lists. Perhaps you meant line[0:3] instead?

However, even assuming that is what you meant, you still need to convert each input line to a list of integers. Try this:

for line in f:
    numbers = [int(n) for n in line.split()]
    grid = [0, 0, 0]
    grid[0] = numbers[0:3]
   ...

There are a few issues to work out...

1) Here, you're comparing a number held as a string (in the grid) to a number held as an integer (passed in ai) - so this function will always return False.

def contains(grid, i):
#Returns true if i appears in the grid & returns false otherwise
    for row in grid:
        for value in row:
            if value == i:
                return true
    return False

Converting the integer to a string to compare will work:

def contains(grid, i):
#Returns true if i appears in the grid & returns false otherwise
    for row in grid:
        for value in row:
            if value == str(i):
                return true
    return False

2) Then , you'll run into the problem that this code throws an error, as you're trying to use the sum function on a string (eg '123') rather than a list of numbers (eg [1,2,3]) here:

r1 = sum(grid[0])
r2 = sum(grid[1])
r3 = sum(grid[2])

You can hack around this with a quick custom sum function:

def string_sum(s):
    return int(s[0]) + int(s[1]) + int(s[2]) 

...and then drop it in...

r1 = string_sum(grid[0])
r2 = string_sum(grid[1])
r3 = string_sum(grid[2])

At this point, it appears to work and (interestingly) give the answers you want, but there are still two problems, one major, one minor:

3) Your logic for the isValidMagicSquare function is broken overall: returning a value from a function ends the function's operation at that point. Thus, as the row-checking section of logic always returns either true or false, you'll never move onto checking the columns or the diagonals - this code is never used. For this, I think you will need a rethink of the logic and structure, which is probably beyond this scope of this question/answer.

4) You've got undeclared variables here:

dL = full[0][0]+ full[1][1]+ full[2][2]
dR = full[0][2]+ full[1][1]+ full[2][0]

I assume full should be grid . But the code will run, as (as said) this section is never reached.

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