程序检查Magic Square python是否

时间:2019-02-01 00:47:50

标签: python

我正在尝试使用python创建一个程序,该程序将基于文本文件确定正方形是否为魔术正方形。

def getRowsAndColumns(user2DimensionalList):
  numberOfRows = len(user2DimensionalList)
  numberOfColumns = len(user2DimensionalList[0])
  
  return numberOfRows, numberOfColumns

def getFirstRowSum(user2DimensionalList, numberOfColumns):
  firstRowSum = 0
  
  for currentRowIndex in range(1):
    for currentColumnIndex in range(numberOfColumns):
      firstRowSum = firstRowSum + user2DimensionalList[currentRowIndex][currentColumnIndex]

    return firstRowSum

def hasEqualRowSums(user2DimensionalList, firstRowSum, numberOfRows, numberOfColumns):
  rowSum = 0
  
  for currentRowIndex in range(numberOfRows):
    for currentColumnIndex in range(numberOfColumns):
      rowSum = rowSum + user2DimensionalList[currentRowIndex][currentColumnIndex]
    if rowSum != firstRowSum:
      return False
    rowSum = 0
    
  return True

def hasEqualColumnSums(user2DimensionalList, firstRowSum, numberOfRows, numberOfColumns):
    columnSum = 0
    for currentColumnIndex in range(numberOfColumns):
      for currentRowIndex in range(numberOfRows):
          columnSum = columnSum + user2DimensionalList[currentRowIndex][currentColumnIndex]
      if columnSum != firstRowSum:
          return False
      columnSum = 0
    
    return True

def hasEqualRowAndColumnSums(user2DimensionalList, firstRowSum, numberOfRows, numberOfColumns):
  if hasEqualRowSums(user2DimensionalList, firstRowSum, numberOfRows, numberOfColumns) and hasEqualColumnSums(user2DimensionalList, firstRowSum, numberOfRows, numberOfColumns):
       return True
  else:
       return False
     
     
def hasFromLeftEqualDiagonalSum(user2DimensionalList, lengthOfAnyRowOrColumn, firstRowSum):
  fromLeftDiagonalSum = 0
  
  for currentDiagonalNumberIndex in range(lengthOfAnyRowOrColumn):
    fromLeftDiagonalSum = fromLeftDiagonalSum + user2DimensionalList[currentDiagonalNumberIndex][currentDiagonalNumberIndex]
        
  if fromLeftDiagonalSum != firstRowSum:
    return False
  else:
    return True
  
def hasFromRightEqualDiagonalSum(user2DimensionalList, lengthOfAnyRowOrColumn, firstRowSum):
    fromRightDiagonalSum = 0
    currentDiagonalNumberColumnIndex = lengthOfAnyRowOrColumn - 1
    
    for currentDiagonalNumberRowIndex in range(lengthOfAnyRowOrColumn):
        fromRightDiagonalSum = fromRightDiagonalSum + user2DimensionalList[currentDiagonalNumberRowIndex][currentDiagonalNumberColumnIndex]
        currentDiagonalNumberColumnIndex = currentDiagonalNumberColumnIndex - 1
    if fromRightDiagonalSum != firstRowSum:
        return False
    else:
        return True
def hasEqualDiagonalSums(user2DimensionalList, lengthOfAnyRowOrColumn, firstRowSum):
    if hasFromLeftEqualDiagonalSum(user2DimensionalList, lengthOfAnyRowOrColumn, firstRowSum) and hasFromRightEqualDiagonalSum(user2DimensionalList, lengthOfAnyRowOrColumn, firstRowSum):
        return True
    else:
        return False

def isLoShuMagicSquare(user2DimensionalList, firstRowSum, numberOfRows, numberOfColumns, lengthOfAnyRowOrColumn):
    if hasEqualRowAndColumnSums(user2DimensionalList, firstRowSum, numberOfRows, numberOfColumns) and hasEqualDiagonalSums(user2DimensionalList, lengthOfAnyRowOrColumn, firstRowSum):
        return True
    else:
        return False


def main():
    user2DimensionalList = open("input.txt", "r")
    user2DimensionalList = user2DimensionalList.read()
   
    
    
    numberOfRows, numberOfColumns = getRowsAndColumns(user2DimensionalList)
    
    lengthOfAnyRowOrColumn = numberOfRows
    
    firstRowSum = getFirstRowSum(user2DimensionalList, numberOfColumns)
    
    if isLoShuMagicSquare(user2DimensionalList, firstRowSum, numberOfRows, numberOfColumns, lengthOfAnyRowOrColumn):
        print("VALID")
    else:
        print("INVALID")
        
main()
    
这是我的txt文件中的内容:

4 3 8 9 5 1 2 7 6
8 3 4 1 5 9 6 7 2
6 1 8 7 5 3 2 9 4
6 9 8 7 5 3 2 1 4
6 1 8 7 5 3 2 1 4
6 1 3 2 9 4 8 7 5
5 5 5 5 5 5 5 5 5

我无法弄清楚我在做什么错了,我得到了这个错误:

Traceback (most recent call last):
  File "/Users/noahhenkle/Desktop/MagicSquare2.py", line 98, in <module>
    main()
  File "/Users/noahhenkle/Desktop/MagicSquare2.py", line 91, in main
    firstRowSum = getFirstRowSum(user2DimensionalList, numberOfColumns)
  File "/Users/noahhenkle/Desktop/MagicSquare2.py", line 12, in getFirstRowSum
    firstRowSum = firstRowSum + user2DimensionalList[currentRowIndex][currentColumnIndex]
TypeError: unsupported operand type(s) for +: 'int' and 'str'

我几乎可以肯定代码是正确的,但是txt文件将所有内容弄乱了。 我在下面包括了追溯。

1 个答案:

答案 0 :(得分:0)

非常简单,您读取了一堆字符数据,而从未转换为整数。然后尝试对数据进行算术运算,从整数值0开始。

在某些时候,您必须转换您的值。目前,您有一个126x1的“ 2Dim列表”。您尝试对行求和是从尝试添加cmd = [bytes(s, 'utf-8').decode('unicode_escape') for s in cmd] 开始。

我强烈建议您恢复到增量编程。您发布了100行代码,而未测试提供程序其余部分的输入。