递归函数本身没有定义

时间:2016-06-03 18:57:41

标签: python recursion nameerror

我正在尝试编写一个程序来解决N Queens Puzzle。当代码尝试递归调用name 'FindPosition' is not defined时,我收到错误FindPosition(finalArray, startArray)。如何正确调用该函数?

class TheQueensAlgoritm():

    def FindPosition(finalArray, startArray):

        if len(startArray)==0:
            print(finalArray)
            return
        else:
            startIndex = 0

            while startIndex <= len(startArray):
                finalIndex = len(finalArray) - 1

                while finalIndex >= 0:                                                        
                    if (not(startArray[startIndex] == finalArray[finalIndex] - finalIndex + startIndex) and not(startArray[startIndex] == finalArray[finalIndex] + finalIndex - startIndex)):
                        finalIndex = finalIndex - 1                    
                    else:
                        break
                    finalArray.append(startArray[startIndex])
                    startArray.remove(finalArray[-1])
                    FindPosition(finalArray, startArray)
                startIndex = startIndex + 1

            startArray.append(finalArray[-1])
            finalArray.remove(startArray[-1])
            FindPosition(finalArray, startArray)        

    startArray = []
    finalArray = []  
    boardSize = input("Type the size of the board = ")
    boardSize = int(boardSize)

    for i in range (0, boardSize):
        startArray.append(i) 

    finalArray.append(startArray[random.randint(0,boardSize-1)])  
    startArray.remove(finalArray[0])                              
    FindPosition(finalArray, startArray)

1 个答案:

答案 0 :(得分:1)

FindPosition是类TheQueensAlgoritm的方法,并不存在。你需要使用:

self.FindPosition(finalArray, startArray)