Jump to content

Hey guys, I'm taking a Raspberry Pi course this semester and I'm looking for some assistance.

 

We are using Python and were tasked with the Fibonacci Sequence. How do I call? How do I get this to work?

Any help is appreciated.

    class Fib:
        def __init__(self, max):
            self.max = max
        
        def __iter__(self):
            self.a = 0
            self.b = 1
            return self
            
        def __next__(self):
            fib = self.a
            if fib > self.max:
                raise StopIteration
            self.a, self.b = self.b, self.a + self.b
            return fib

 

Link to comment
https://linustechtips.com/topic/893307-python-fibonacci-help/
Share on other sites

Link to post
Share on other sites

def main():
      x = Fib(max)
      x.__iter__()
      x.__next()

 

This code added below the rest of it results in the following message in the shell. Nothing happens.

>>>> %Run FibLab3.py

>>>>

Edit: I added print(x.__next__()) doesn't seem to print anything.

Also, I'm understanding this a lot more now. The x becomes an object of the class that comes after the dot. Thank you, you are being very helpful.

adding

Link to comment
https://linustechtips.com/topic/893307-python-fibonacci-help/#findComment-11011738
Share on other sites

Link to post
Share on other sites

19 minutes ago, Sdot said:

def main():
      x = Fib(max)
      x.__iter__()
      x.__next()

 

This code added below the rest of it results in the following message in the shell. Nothing happens.

>>>> %Run FibLab3.py

>>>>

Edit: I added print(x.__next__()) doesn't seem to print anything.

Also, I'm understanding this a lot more now. The x becomes an object of the class that comes after the dot. Thank you, you are being very helpful.

adding

You need to specify a number for max, like Fib(100). Then the program will stop when the Fibonacci number exceeds that value

Link to comment
https://linustechtips.com/topic/893307-python-fibonacci-help/#findComment-11011783
Share on other sites

Link to post
Share on other sites

15 minutes ago, Sdot said:

Does this look correct?


for i in range(100):
      print(x.__next__())

Sorry cant format cause Im on mobile.

You wrote an iterator, so use it like this:

 

for i in Fib(100):

    print(i)

Desktop: Intel i9-10850K (R9 3900X died 😢 )| MSI Z490 Tomahawk | RTX 2080 (borrowed from work) - MSI GTX 1080 | 64GB 3600MHz CL16 memory | Corsair H100i (NF-F12 fans) | Samsung 970 EVO 512GB | Intel 665p 2TB | Samsung 830 256GB| 3TB HDD | Corsair 450D | Corsair RM550x | MG279Q

Laptop: Surface Pro 7 (i5, 16GB RAM, 256GB SSD)

Console: PlayStation 4 Pro

Link to comment
https://linustechtips.com/topic/893307-python-fibonacci-help/#findComment-11011936
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×