[英]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.洛书魔方是一个3行3列的方格,如下图。 The Lo Shu Magic Square has the following properties:洛书魔方具有以下属性:
The grid contains the numbers 1 through 9 exactly.网格正好包含数字 1 到 9。
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.编写一个function,接受一个二维列表作为参数,并判断该列表是否为洛书魔方。 Test the function in a program.在程序中测试 function。 4 9 2 3 5 7 8 1 6 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.出于测试目的,您应该发送两个二维列表:一个是 Lo Shu Magic Square,另一个不是。
I am trying to get the program to return as True, but even with the correct numbers above it still outputs False everytime.我试图让程序返回 True,但即使上面有正确的数字,它仍然每次都输出 False。 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.所以这只是代码中的一些拼写错误,第一个示例是 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.返回 36 作为第一行总和,这就是为什么它最后总是返回 false。 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.它将返回 15。下一个问题是 eqDiagonalSums() 在那里,您实际上创建了一个递归 function ,它无限地调用自己,您只是没有意识到它,因为您的代码从未达到这一点。 finally you had given leftEqDiagonalSum() a tuple instead of:最后你给了 leftEqDiagonalSum() 一个元组,而不是:
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()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.