【问题标题】:Coding Fibonacci in python. Got errors在 python 中编码斐波那契。出现错误
【发布时间】:2021-11-03 19:05:59
【问题描述】:

我正在为 AI 和 Python 的初学者做一个作业。

创建一个继承自 int 的类 NewInt。它应该有一个实例方法 is_fibonacci (),如果数字是斐波那契数,则返回 True,否则返回 False。使用 NewInt 从 0 到 1000 生成一个列表。然后使用您创建的类和实例方法创建一个仅保留斐波那契数字的列表理解。

[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 新手。

【问题讨论】:

  • 您在这里有些混淆实例和静态方法。你可以试试NewInt.is_fibonacci(i),但整个NewInt 类并没有那么大的意义。另外,为什么不直接生成斐波那契数列,而不是测试每个数字是否是斐波那契数呢?
  • 您的代码缩进不正确。
  • 你的类方法需要self的第一个参数来包含实例引用。

标签: python list fibonacci


【解决方案1】:
  • 这是python的OOP概念

    NewInt().is_fibonacci(i)

  • 当上面的方法被python内部调用时

    NewInt().is_fibonacci(self, i)

  • self 指的是本例中调用该方法的对象NewInt()

  • 但在类定义中,该方法只为一个参数定义

TypeError: is_fibonacci() 接受 1 个位置参数,但给出了 2 个

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)

【讨论】:

  • 感谢您的反馈。它有帮助。
【解决方案2】:
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]

如果你总是在函数的形参中取值,那么有一个类是没有意义的。

【讨论】:

  • 感谢您的反馈 没什么特别的。很高兴得到建议。我很高兴看到有几种方法可以解决这个问题。 :)
猜你喜欢
  • 2010-09-22
  • 1970-01-01
  • 2013-03-04
  • 1970-01-01
  • 1970-01-01
  • 2014-05-23
  • 1970-01-01
  • 1970-01-01
  • 2011-05-07
相关资源
最近更新 更多