![](/img/trans.png)
[英]Lo Shu Magic Square - Keeps showing 'false' is not defined and then Invalid
[英]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.