簡體   English   中英

洛書魔方總是返回假

[英]Lo Shu Magic Square always returning false

這就是我想要做的:

洛書魔方是一個3行3列的方格,如下圖。 洛書魔方具有以下屬性:

網格正好包含數字 1 到 9。

每行、每列和每條對角線的總和都加起來相同的數字。

在程序中,您可以使用二維列表模擬幻方。 編寫一個function,接受一個二維列表作為參數,並判斷該列表是否為洛書魔方。 在程序中測試 function。 4 9 2 3 5 7 8 1 6

出於測試目的,您應該發送兩個二維列表:一個是 Lo Shu Magic Square,另一個不是。

我試圖讓程序返回 True,但即使上面有正確的數字,它仍然每次都輸出 False。 任何幫助都會很大這是我的代碼:

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() 

所以這只是代碼中的一些拼寫錯誤,第一個示例是 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

返回 36 作為第一行總和,這就是為什么它最后總是返回 false。 代碼實際上應該是。

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

它將返回 15。下一個問題是 eqDiagonalSums() 在那里,您實際上創建了一個遞歸 function ,它無限地調用自己,您只是沒有意識到它,因為您的代碼從未達到這一點。 最后你給了 leftEqDiagonalSum() 一個元組,而不是:

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

這是運行良好的完整代碼。 如果您有任何問題,請告訴我。

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() 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM