Python Forum

Full Version: Help debug my fibonacci implementation
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello everyone . I was getting my hands on Python to learn Data structures and algorithms in python. I tried to implement progression class with sub classes : AP , GP and fibonacci… the implementation of AP and GP works well but fibonacci doesnt work right. Here is the code:
class progression:
    def __init__(self,start):
        self.start = start
        self.x = str()
        self.answer = int()
    def __next__(self):
        self.answer = self.start
        self.start += 1
        return self.answer
    def __iter__(self):
        return self

    def printer(self,n):
        for i in range(0,n):
            self.x = self.x + ' ' + ''.join(str(next(self)))

        print(self.x)

class arithematic(progression):
    def __init__(self,start,inc):
        super().__init__(start)
        self.inc = inc

    def __next__(self):
        self.answer = self.start
        self.start += self.inc
        return self.answer

class gp(progression):
    def __init__(self,start,factor):
        super().__init__(start)
        self.factor = factor

    def __next__(self):
        self.answer = self.start
        self.start *= self.factor
        return self.answer

class fibo(progression):
    def __init__(self,first,second):
        super().__init__(first)
        self.prev = second - first

    def next(self):
        self.answer = self.start
        self.start += self.prev
        self.prev = self.answer
        return self.answer



obj = fibo(1,3)
obj.printer(10)
Help me debug the program please! i cannot seem to find the error
doesn't work right doesn't tell us much.
do you get ant error? if yes - post the full traceback in error tags
if the result is not what you expect - post what you get and what you expect as output
It does not give any error . Runs fine but it gives following output:

1 2 3 4 5 6 7 8 9 10

It simply gives an output of simple progression class.
i expect following output:
1 3 4 7 11 18 29 upto the 10th term
rename fibo.next() method to fibo.__next__()
(May-16-2018, 11:51 AM)buran Wrote: [ -> ]rename fibo.next() method to fibo.__next__()

That is not the solution since printer function is working right. the problem must be in class fibonacci since other progressions are working fine
did you try what I suggested or not?
here is screenshot of my pythonanywhere account and the output
https://screenshots.firefox.com/h4FDkAJo...ywhere.com

note that you don't have class fibonaci, your class is fibo
(May-16-2018, 12:09 PM)buran Wrote: [ -> ]did you try what I suggested or not?
here is screenshot of my pythonanywhere account and the output
https://screenshots.firefox.com/h4FDkAJo...ywhere.com

note that you don't have class fibonaci, your class is fibo

O my god , that was such a stupid mistake from my part. Thank you so much ! i really appreciate you giving my problem time.
That solved the problem ! Smile