这就是我想做的:
罗叔魔方是一个3行3列的网格,如下图所示。罗树魔方具有以下特性:
网格正好包含数字1到9
每一行、每一列和每一对角线的总和加起来等于同一个数字
在程序中,您可以使用二维列表模拟幻方。编写一个函数,该函数接受二维列表作为参数,并确定该列表是否为Lo Shu幻方。在程序中测试功能。 4 9 2 3 5 7 816
出于测试目的,您应该发送两个二维列表:一个是罗叔魔方,另一个不是
我试图让程序返回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()函数
返回36作为第一行总和,这就是为什么它最后总是返回false。代码实际上应该是
它将返回15。下一个问题是eqDiagonalSums(),在这里,您实际上创建了一个递归函数,它可以无限地调用自身,但您没有意识到这一点,因为您的代码从未达到这一点。最后,您给了leftEqDiagonalSum()一个元组,而不是:
下面是运行正常的完整代码。如果你有任何问题,请告诉我
相关问题 更多 >
编程相关推荐