Python Forum
Help debug my fibonacci implementation
Thread Rating:
  • 1 Vote(s) - 2 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help debug my fibonacci implementation
#1
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
Reply
#2
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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
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
Reply
#4
rename fibo.next() method to fibo.__next__()
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
(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
Reply
#6
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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#7
(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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  pycharm debug help mg24 1 1,032 Nov-18-2022, 05:38 AM
Last Post: deanhystad
  Python debug suddenly got bad ben1122 3 1,098 Sep-03-2022, 06:20 AM
Last Post: ben1122
Bug Help Debug please jamesaarr 12 3,865 Jul-28-2021, 11:20 PM
Last Post: Pedroski55
  Fibonacci Yasunaga 7 2,930 May-16-2021, 02:36 PM
Last Post: csr
  Error in Int object is not subscript-able. How to debug this ? yanDvator 1 2,227 Aug-03-2020, 02:28 PM
Last Post: Larz60+
  is there a debug mode available while creating python egg BhushanPathak 1 2,370 Dec-19-2018, 04:15 PM
Last Post: Larz60+
  fibonacci ***Time limit exceeded*** frequency 18 10,202 Nov-29-2018, 09:03 PM
Last Post: frequency
  Fibonacci sequence Darbandiman123 2 2,700 Sep-26-2018, 02:32 PM
Last Post: Darbandiman123
  Not sure how to debug this? rsmldmv 3 5,445 Nov-09-2017, 02:51 AM
Last Post: snippsat
  Debug and trace in python quocchi22101262 0 3,492 Jun-20-2017, 09:35 AM
Last Post: quocchi22101262

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020