Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typo3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/facebook/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 为什么在第18行出现错误:AttributeError:';光纤计数器';对象没有属性';光纤计数器';发生 类计数器: 定义内部(自我): self.Fibcounter=0 def getCount(自身): 返回自我计数器 def重置计数(自身): self.Fibcounter=0 返回自我计数器 def fib(自身,n): self.Fibcounter=self.Fibcounter+1 如果n_Python - Fatal编程技术网

Python 为什么在第18行出现错误:AttributeError:';光纤计数器';对象没有属性';光纤计数器';发生 类计数器: 定义内部(自我): self.Fibcounter=0 def getCount(自身): 返回自我计数器 def重置计数(自身): self.Fibcounter=0 返回自我计数器 def fib(自身,n): self.Fibcounter=self.Fibcounter+1 如果n

Python 为什么在第18行出现错误:AttributeError:';光纤计数器';对象没有属性';光纤计数器';发生 类计数器: 定义内部(自我): self.Fibcounter=0 def getCount(自身): 返回自我计数器 def重置计数(自身): self.Fibcounter=0 返回自我计数器 def fib(自身,n): self.Fibcounter=self.Fibcounter+1 如果n,python,Python,您缺少一个i: class FibCounter: def __int__(self): self.Fibcounter = 0 def getCount(self): return self.Fibcounter def resetCount(self): self.Fibcounter = 0 return self.Fibcounter def fib(self,n):

您缺少一个
i

class FibCounter:

    def __int__(self):
        self.Fibcounter = 0

    def getCount(self):
        return self.Fibcounter

    def resetCount(self):
        self.Fibcounter = 0
        return self.Fibcounter

    def fib(self,n):
        self.Fibcounter = self.Fibcounter + 1
        if n<3:
            return 1
        else:
            return fib(n-1)+fib(n-2)

def main():
    n = eval(input("Enter the value of n (n represents the nth Fibonacci number):" ))
    Fibonacci = FibCounter()
    Fibonacci.fib(n)
    print("The number of time fib function is called is:",Fibonacci.getCount())
    Fibonacci.resetCount()

if __name__ == '__main__': 
    main()
你想要

def __int__(self):
这就是为什么没有设置
Fibcounter
;您的
\uuuu int\uuu
函数从未被调用

(请注意,
Fibcounter
不是
Fibcounter
类中变量的好名称,因此您可能需要更改它。)


在此之后,还有一些其他问题需要解决(
fib
将无法调用自身,例如)。

这似乎与主题无关,因为它是由无法再复制的问题或简单的印刷错误引起的。
getCount()
没有意义,因为您可以直接访问该属性(在Python中应该这样做,不像在Java中那样。)和
resetCount()
不应该返回
self.Fibcounter
,因为这样做毫无意义;只需省略
return
指令即可(隐式地)return
None
。请解释fib无法调用自身的原因好吗?谢谢!@user3478368:试试。您需要通过
self.fib
引用该方法。
def __init__(self):