我正在尝试编写一个类来使用递归解决阶乘。
这是我的代码:
class Factorial:
def __init__(self,n):
self.n = n
def factorial(self):
if self.n == 0:
return 1
else:
return self.n * self.factorial(self.n - 1)
我这样称呼它:
fact = Factorial(4)
fact.factorial()
我收到此错误:
Traceback (most recent call last):
File "factorial.py", line 13, in <module>
fact.factorial()
File "factorial.py", line 10, in factorial
return self.n * self.factorial(self.n - 1)
TypeError: factorial() takes exactly 1 argument (2 given)
谢谢