递归函数找到fibonnaci系列

时间:2018-03-08 09:10:06

标签: python recursion

我想让这段代码能够找到带有递归的斐波纳契系列,但我不明白为什么我的代码没有这样做

class fib: #fibonacci series 
    a = 0
    b = 0
    c = 0
    count = 0
    def __init__(self, a , b,count):
        self.a=a
        self.b=b
        self.count=count;

    def fibb(self, a , b,count):
        if(self.count > count):
            return;
        else:
            if(self.count == 0):
                print a
                print b
                self.count = self.count + 1
                print " counting : ", self.count
                self.c = self.a + self.b
                self.a = self.b
                self.b = self.c
                #print self.a , ", ",self.b
                return fibb(self.a,self.b,count)
            else:
                print b
                self.count = self.count + 1
                print " counting : ", self.count
                self.c = self.a + self.b
                self.a = self.b
                self.b = self.c
                #print self.a , ", ",self.b
                return fibb(self.a,self.b,count)
fib1 = fib(1,2,0) # creating object
fib1.fibb(1,1,10)  # calling function
My Output:

Traceback (most recent call last):
File "jdoodle.py", line 36, in 
fib1.fibb(1,1,10)  # calling function
File "jdoodle.py", line 24, in fibb
return fibb(self.a,self.b,count)
NameError: global name 'fibb' is not defined
Command exited with non-zero status 1

1 个答案:

答案 0 :(得分:0)

引用该功能时使用self.fibb。 Python没有明确地知道在类

中调用fibb函数