在 python 中编码斐波那契数列。有错误
Coding Fibonacci in python. Got errors
我正在为 AI 初学者做一个作业 Python。
创建一个 Class 继承自 int 的 NewInt。它应该有一个实例方法 is_fibonacci () 如果数字是斐波那契数则 returns 为真,否则为假。使用 NewInt 生成从 0 到 1000 的列表。然后使用您创建的 class 和实例方法创建一个仅保留斐波那契数字的列表理解。
[0, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987] #任务的预期输出
我为任务编写了这段代码
import math
class NewInt(int):
def is_Perfect_Square(x):
s = int(math.sqrt(x))
return s*s == x
def is_fibonacci(n):
return is_Perfect_Square(5*n*n + 4) or is_Perfect_Square(5*n*n - 4)
fibonacci_List = [i for i in range(0,1000)if NewInt().is_fibonacci(i)]
print(fibonacci_List)
它在几个小时前工作,但我收到这样的错误:
TypeError Traceback (most recent call last)
<ipython-input-8-377e0f9b7814> in <module>
1 import math
2
----> 3 class NewInt(int):
4
5 def is_Perfect_Square(x):
<ipython-input-8-377e0f9b7814> in NewInt()
11 return is_Perfect_Square(5*n*n + 4) or is_Perfect_Square(5*n*n - 4)
12
---> 13 fibonacci_List = [i for i in range(0,1000)if NewInt().is_fibonacci(i)]
14 print(fibonacci_List)
<ipython-input-8-377e0f9b7814> in <listcomp>(.0)
11 return is_Perfect_Square(5*n*n + 4) or is_Perfect_Square(5*n*n - 4)
12
---> 13 fibonacci_List = [i for i in range(0,1000)if NewInt().is_fibonacci(i)]
14 print(fibonacci_List)
TypeError: is_fibonacci() takes 1 positional argument but 2 were given
有人可以帮我指出我的错误吗?我是 Python.
的新手
import math
class NewInt(int):
# In my opinion you should make a constructor
def __init__(self, number):
# Declare the attributes
self.number = number
def is_Perfect_Square(self):
# Use that attribute which u have initialised already.
x = self.number
s = int(math.sqrt(x))
return s * s == x
def is_fibonacci(self):
n = self.number
# Create an instance of your class each time u wanna access a method
# e.g. NewInt(1234) creates an instant of the class NewInt. When you do this, u invoke the init method and that number attribute is intialised.
return NewInt(5*n*n + 4).is_Perfect_Square() or NewInt(5 * n * n - 4).is_Perfect_Square()
fibonacci_List = [i for i in range(0, 1000) if NewInt(i).is_fibonacci()]
print(fibonacci_List)
输出:
[0, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987]
如果您总是在函数的形参中取值,那么 class.
就没有意义了
这是python的 OOP 概念
NewInt().is_fibonacci(i)
当上面的方法被调用时python在内部做这个
NewInt().is_fibonacci(self, i)
self指的是本例中调用方法的对象NewInt()
但在 class 定义中,该方法仅针对一个参数定义,因此
TypeError: is_fibonacci() takes 1 positional argument but 2 were given
import math
class NewInt(int):
def is_Perfect_Square(self, x):
s = int(math.sqrt(x))
return s*s == x
def is_fibonacci(self, n):
return self.is_Perfect_Square(5*n*n + 4) or self.is_Perfect_Square(5*n*n - 4)
fibonacci_List = [i for i in range(0,1000)if NewInt().is_fibonacci(i)]
print(fibonacci_List)
我正在为 AI 初学者做一个作业 Python。
创建一个 Class 继承自 int 的 NewInt。它应该有一个实例方法 is_fibonacci () 如果数字是斐波那契数则 returns 为真,否则为假。使用 NewInt 生成从 0 到 1000 的列表。然后使用您创建的 class 和实例方法创建一个仅保留斐波那契数字的列表理解。
[0, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987] #任务的预期输出
我为任务编写了这段代码
import math
class NewInt(int):
def is_Perfect_Square(x):
s = int(math.sqrt(x))
return s*s == x
def is_fibonacci(n):
return is_Perfect_Square(5*n*n + 4) or is_Perfect_Square(5*n*n - 4)
fibonacci_List = [i for i in range(0,1000)if NewInt().is_fibonacci(i)]
print(fibonacci_List)
它在几个小时前工作,但我收到这样的错误:
TypeError Traceback (most recent call last)
<ipython-input-8-377e0f9b7814> in <module>
1 import math
2
----> 3 class NewInt(int):
4
5 def is_Perfect_Square(x):
<ipython-input-8-377e0f9b7814> in NewInt()
11 return is_Perfect_Square(5*n*n + 4) or is_Perfect_Square(5*n*n - 4)
12
---> 13 fibonacci_List = [i for i in range(0,1000)if NewInt().is_fibonacci(i)]
14 print(fibonacci_List)
<ipython-input-8-377e0f9b7814> in <listcomp>(.0)
11 return is_Perfect_Square(5*n*n + 4) or is_Perfect_Square(5*n*n - 4)
12
---> 13 fibonacci_List = [i for i in range(0,1000)if NewInt().is_fibonacci(i)]
14 print(fibonacci_List)
TypeError: is_fibonacci() takes 1 positional argument but 2 were given
有人可以帮我指出我的错误吗?我是 Python.
的新手import math
class NewInt(int):
# In my opinion you should make a constructor
def __init__(self, number):
# Declare the attributes
self.number = number
def is_Perfect_Square(self):
# Use that attribute which u have initialised already.
x = self.number
s = int(math.sqrt(x))
return s * s == x
def is_fibonacci(self):
n = self.number
# Create an instance of your class each time u wanna access a method
# e.g. NewInt(1234) creates an instant of the class NewInt. When you do this, u invoke the init method and that number attribute is intialised.
return NewInt(5*n*n + 4).is_Perfect_Square() or NewInt(5 * n * n - 4).is_Perfect_Square()
fibonacci_List = [i for i in range(0, 1000) if NewInt(i).is_fibonacci()]
print(fibonacci_List)
输出:
[0, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987]
如果您总是在函数的形参中取值,那么 class.
就没有意义了这是python的 OOP 概念
NewInt().is_fibonacci(i)
当上面的方法被调用时python在内部做这个
NewInt().is_fibonacci(self, i)
self指的是本例中调用方法的对象
NewInt()
但在 class 定义中,该方法仅针对一个参数定义,因此
TypeError: is_fibonacci() takes 1 positional argument but 2 were given
import math
class NewInt(int):
def is_Perfect_Square(self, x):
s = int(math.sqrt(x))
return s*s == x
def is_fibonacci(self, n):
return self.is_Perfect_Square(5*n*n + 4) or self.is_Perfect_Square(5*n*n - 4)
fibonacci_List = [i for i in range(0,1000)if NewInt().is_fibonacci(i)]
print(fibonacci_List)