简体   繁体   中英

Lo Shu Magic Square always returning false

This is what I am trying to do:

The Lo Shu Magic Square is a grid with 3 rows and 3 columns, as shown below. The Lo Shu Magic Square has the following properties:

The grid contains the numbers 1 through 9 exactly.

The sum of each row, each column, and each diagonal all add up to the same number.

In a program you can simulate a magic square using a two-dimensional list. Write a function that accepts a two-dimensional list as an argument and determines whether the list is a Lo Shu Magic Square. Test the function in a program. 4 9 2 3 5 7 8 1 6

For testing purposes you should send in two two-dimensional lists: one is a Lo Shu Magic Square, the other is not.

I am trying to get the program to return as True, but even with the correct numbers above it still outputs False everytime. Any help would be greatly a Here is my code:

def numOfRowsAndColumns(inp2DList):
    numOfRows = len(inp2DList)
    numOfColumns = len(inp2DList[0])
    return numOfRows, numOfColumns

#This function processes the individual numbers by row and column by adding
#them together.
def getRowSum(inp2DList, numOfColumns):
    firstRowSum = 0
    for currentRow in range (1):
        for currentColumn in range (numOfColumns):
            firstRowSum += firstRowSum + inp2DList[currentRow][currentColumn]
    return firstRowSum

#These functions returns a true/false value based on equality of Rows & Columns.
def equalRows (inp2DList, firstRowSum, numOfRows, numOfColumns):
    rowSum = 0
    for currentRow in range (numOfRows):
        for currentColumn in range (numOfColumns):
            rowSum = rowSum + inp2DList[currentRow][currentColumn]
        if rowSum != firstRowSum: 
            return False
        rowSum = 0      #resets rowSum to 0
    return True

def equalColumns (inp2DList, firstRowSum, numOfRows, numOfColumns):
    columnSum = 0
    for currentColumn in range (numOfColumns):
        for currentRow  in range (numOfRows):
            columnSum = columnSum + inp2DList[currentRow][currentColumn]
        if columnSum != firstRowSum:
            return False
        columnSum = 0       #resets columnSum to 0
    return True

#This function acts as a check against the previsous equalColumns &
#equalRows functions.
def equalRCSums(inp2DList,firstRowSum,numOfRows,numOfColumns):
    if equalRows(inp2DList,firstRowSum,numOfRows,numOfColumns)\
         and equalColumns(inp2DList, firstRowSum,numOfRows, numOfColumns):
        return True
    else:
        return False
#These functions do the same thing as the previous ones, except diagonally.
def leftEqDiagonalSum(inp2DList,randLengthAnyRC,firstRowSum):
    leftDiagonalSum = 0
    for currentDiagonalNum in range (randLengthAnyRC):
        leftDiagonalSum = leftDiagonalSum + \
            inp2DList[leftEqDiagonalSum, currentDiagonalNum]
    if leftDiagonalSum != firstRowSum:
        return False
    else:
        return True

def rightEqDiagonalSum(inp2DList,randLengthAnyRC,firstRowSum):
    rightDiagonalSum = 0
    currentDiagonalNumColumn = randLengthAnyRC - 1
    for currentDiagonalNumRow in range (randLengthAnyRC):
        rightDiagonalSum = rightDiagonalSum + inp2DList[currentDiagonalNumColumn][currentDiagonalNumRow]
        currentDiagonalNumColumn = currentDiagonalNumColumn - 1
    if rightDiagonalSum != firstRowSum:
        return False
    else:
        return True

#This function returns true or false based on the diagonal sums of the
#numbers in the list.
def eqDiagonalSums(inp2DList, randLengthAnyRC, firstRowSum):
    if eqDiagonalSums(inp2DList,randLengthAnyRC, firstRowSum)\
         and eqDiagonalSums(inp2DList, randLengthAnyRC, firstRowSum):
        return True
    else:
        return False

#This functuon determines if the list given is a Lo Shu Magic Sqaure.
def isThisALoShu(inp2DList, firstRowSum, numOfRows, numOfColumns, randLengthAnyRC):
    if equalRCSums(inp2DList, firstRowSum, numOfRows, numOfColumns)\
        and eqDiagonalSums(inp2DList, randLengthAnyRC, firstRowSum):
        return True
    else:
        return False



#This main function takes the sample list and will tell you if it's
#a magic square or not.
def main():
    inp2DList = [[4,9,2],[3,5,7],[8,1,6]]
    numOfRows, numOfColumns = numOfRowsAndColumns(inp2DList)
    randLengthAnyRC = numOfRows
    firstRowSum = getRowSum(inp2DList, numOfColumns)
    if isThisALoShu(inp2DList, firstRowSum, numOfRows, numOfColumns,randLengthAnyRC):
        print("This is a Lo Shu Magic Square!")
    else:
        print("This is NOT a Lo Shu Magic Square, please try again.")

main() 

So it was just a few typos in the code the first example is the getRowSum() function.

def getRowSum(inp2DList, numOfColumns):
    firstRowSum = 0
    for currentRow in range (1):
        for currentColumn in range (numOfColumns):
            firstRowSum += firstRowSum + inp2DList[currentRow][currentColumn]
    return firstRowSum

returns 36 as the first row sum which is why it always returns false in the end. the code should actually be.

def getRowSum(inp2DList, numOfColumns):
    firstRowSum = 0
    for currentRowIndex in range(1):
        for currentColumnIndex in range(numOfColumns):
            firstRowSum = firstRowSum + inp2DList[currentRowIndex][currentColumnIndex]
    return firstRowSum

which will return 15. The next issue was with eqDiagonalSums() in there you actually created a recursive function that calls itself infinitely you just didnt realize it because your code never got to that point. finally you had given leftEqDiagonalSum() a tuple instead of:

for currentDiagonalNumber in range (randLengthAnyRC):
    leftDiagonalSum = leftDiagonalSum +\
        inp2DList[currentDiagonalNumber][currentDiagonalNumber]

Here is the full code that ran just fine. Let me know if you have any questions.

def numOfRowsAndColumns(inp2DList):
    numOfRows = len(inp2DList)
    numOfColumns = len(inp2DList[0])
    return numOfRows, numOfColumns

#This function processes the individual numbers by row and column by adding
#them together.
def getRowSum(inp2DList, numOfColumns):
    firstRowSum = 0
    for currentRow in range(1):
        for currentColumn in range(numOfColumns):
            firstRowSum = firstRowSum + inp2DList[currentRow][currentColumn]
    return firstRowSum

#These functions returns a true/false value based on equality of Rows & Columns.
def equalRows (inp2DList, firstRowSum, numOfRows, numOfColumns):
    rowSum = 0
    for currentRow in range(numOfRows):
        for currentColumn in range (numOfColumns):
            rowSum = rowSum + inp2DList[currentRow][currentColumn]
        if rowSum != firstRowSum:
            return False
        rowSum = 0      #resets rowSum to 0
    return True

def equalColumns (inp2DList, firstRowSum, numOfRows, numOfColumns):
    columnSum = 0
    for currentColumn in range(numOfColumns):
        for currentRow  in range (numOfRows):
            columnSum = columnSum + inp2DList[currentRow][currentColumn]
        if columnSum != firstRowSum:
            return False
        columnSum = 0       #resets columnSum to 0
    return True

#This function acts as a check against the previsous equalColumns &
#equalRows functions.
def equalRCSums(inp2DList,firstRowSum,numOfRows,numOfColumns):
    if equalRows(inp2DList,firstRowSum,numOfRows,numOfColumns)\
         and equalColumns(inp2DList, firstRowSum,numOfRows, numOfColumns):
        return True
    else:
        return False
#These functions do the same thing as the previous ones, except diagonally.
def leftEqDiagonalSum(inp2DList,randLengthAnyRC,firstRowSum):
    leftDiagonalSum = 0
    for currentDiagonalNumber in range (randLengthAnyRC):
        leftDiagonalSum = leftDiagonalSum +\
            inp2DList[currentDiagonalNumber][currentDiagonalNumber]
    if leftDiagonalSum != firstRowSum:
        return False
    else:
        return True

def rightEqDiagonalSum(inp2DList,randLengthAnyRC,firstRowSum):
    rightDiagonalSum = 0
    currentDiagonalNumColumn = randLengthAnyRC - 1
    for currentDiagonalNumRow in range (randLengthAnyRC):
        rightDiagonalSum = rightDiagonalSum + inp2DList[currentDiagonalNumColumn][currentDiagonalNumRow]
        currentDiagonalNumColumn = currentDiagonalNumColumn - 1
    if rightDiagonalSum != firstRowSum:
        return False
    else:
        return True

#This function returns true or false based on the diagonal sums of the
#numbers in the list.
def eqDiagonalSums(inp2DList, randLengthAnyRC, firstRowSum):
    if rightEqDiagonalSum(inp2DList,randLengthAnyRC, firstRowSum)\
         and leftEqDiagonalSum(inp2DList, randLengthAnyRC, firstRowSum):
        return True
    else:
        return False

#This functuon determines if the list given is a Lo Shu Magic Sqaure.
def isThisALoShu(inp2DList, firstRowSum, numOfRows, numOfColumns, randLengthAnyRC):
    if equalRCSums(inp2DList, firstRowSum, numOfRows, numOfColumns)\
        and eqDiagonalSums(inp2DList, randLengthAnyRC, firstRowSum):
        return True
    else:
        return False



#This main function takes the sample list and will tell you if it's
#a magic square or not.
def main():
    inp2DList = [[4,9,2],[3,5,7],[8,1,6]]
    numOfRows, numOfColumns = numOfRowsAndColumns(inp2DList)
    randLengthAnyRC = numOfRows
    firstRowSum = getRowSum(inp2DList, numOfColumns)
    if isThisALoShu(inp2DList, firstRowSum, numOfRows, numOfColumns,randLengthAnyRC):
        print("This is a Lo Shu Magic Square!")
    else:
        print("This is NOT a Lo Shu Magic Square, please try again.")

main() 

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