0

There is a assigment I am doing for Beginners in AI and Python.

Create a Class NewInt that inherits from int. It should have an Instance Method is_fibonacci () that returns True if the number is a Fibonacci number, False if not. Generate a list with NewInt from 0 to 1000. Then create a List Comprehension that only retains the numbers that are Fibonacci using the class and instance method you created.

[0, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987] #Expected output for task

I made this code for the task

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)

It worked few hours ago, but i get errors like this:

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

Can someone help me point out my mistakes? I am new to Python.

3
  • You are somewhat confusing instance and static methods here. You can try NewInt.is_fibonacci(i), but the whole NewInt class does not make all that much sense. Also, why not just generate the fibonacci sequence instead of testing each number whether it is a fibonacci number? Commented Sep 6, 2021 at 18:21
  • Indentation is incorrect in your code. Commented Sep 7, 2021 at 2:10
  • Your class methods need a first argument of self to contain the instance reference. Commented Sep 7, 2021 at 2:12

2 Answers 2

2
  • This is pythons OOP concept

    NewInt().is_fibonacci(i)

  • When the above method is called python internally does this

    NewInt().is_fibonacci(self, i)

  • self refers to the object that invoked the method in this case NewInt()

  • But in the class definition the method is defined for only one parameter hence

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)
Sign up to request clarification or add additional context in comments.

Comments

1
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)

Output:

[0, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987]

If you are always taking the value in the formal parameter of your function, it doesn't make sense to have a class.

1 Comment

Thanks for the feedback Nothing special. It's nice to get advice. I cool to see there is severals way to solve this problem. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.