Hi,
I cannot figure out why my fib problem is giving me the output below. I just want it to print when the fib is called and the value of n, when it returns and with what value of n, and the return value.


This is what I have so far:

import math
def fib(n):
    print "Computing fib:", n
    if 0 < n < 3:
        value = 1
        print "Returning value: ", value
    elif n == 0:
        value = 0
        print "Returning value: ", value
    else:
        value = fib(n-1)+fib(n-2)
    return value
                     
def main():
    n = input("Enter a positive number: ")
    fib(n)
main()

Why does it keep returning to the first print statement? I don't understand what I am doing wrong. Plus I did have it printing out the return value of fib(4) or 3 -- at the end of the output but now I can"t even get it to do that. Can someone help please.

This is my output:

====
>>> 
Enter a positive number: 4
Computing fib: 4
Computing fib: 3
Computing fib: 2
Returning value:  1
Computing fib: 1
Returning value:  1
Computing fib: 2
Returning value:  1
>>>

Thanks!

This puts little more light on it:

def fib(n):
    print "Computing fib:", n
    if 0 < n < 3:
        value = 1
        print "1Returning value: ", value
    elif n == 0:
        value = 0
        print "2Returning value: ", value
    else:
        value = fib(n-1)+fib(n-2)
        print "3Returning value: ", value
    return value
                     
def main():
    n = input("Enter a positive number: ")
    print fib(n)

main()

Not sure if this is what you want to do or not, but try replacing

else:
        value = fib(n-1)+fib(n-2)
## with
   else:
        value = fib(n-1)

You might also want to use a list to store the two fib numbers, since fibs are not sequential. Again, not sure what you are trying to do.

I think your code is actually working correctly, if I'm understanding what you want to do. The only thing is that you needed to print the result of fib(n):

def fib(n):
    print "Computing fib:", n
    if 0 < n < 3:
        value = 1
        print "Returning value: ", value
    elif n == 0:
        value = 0
        print "Returning value: ", value
    else:
        value = fib(n-1)+fib(n-2)
    return value
    
def main():
    n = input("Enter a positive number: ")
    [B]print[/B] fib(n)    # <----
main()

Here's the output:

fib(6)
Computing fib: 6
Computing fib: 5
Computing fib: 4
Computing fib: 3
Computing fib: 2
Returning value:  1
Computing fib: 1
Returning value:  1
Computing fib: 2
Returning value:  1
Computing fib: 3
Computing fib: 2
Returning value:  1
Computing fib: 1
Returning value:  1
Computing fib: 4
Computing fib: 3
Computing fib: 2
Returning value:  1
Computing fib: 1
Returning value:  1
Computing fib: 2
Returning value:  1
8

Did you want to do something fancier, like print the values of the fib series along the way?

Jeff

Thank You Vrey Much!! That is exactly what I wanted to do.

I could not figure out why my program was printing an extra print statement but with all of your help I finally see what is wrong with my problem. I had an extra print "statement" in my last section of the progarm.

It probably sounds silly to someone who is good at computer programming but I tried for hours to figure out what was wrong.

This website is very helpful for someone who is learning how to program. Thank you all very much!

I can finally say, "Problem Solved!"

This question has already been answered. Start a new discussion instead.