Public Group

# python oddity

This topic is 4880 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I'm running this function from the IDE if python. def fib(n): print ' n =', n if n > 1: return n * fib(n-1) else: print 'end of the line' return 1 it gives me this output for fib(5) n = 5 n = 4 n = 3 n = 2 n = 1 end of the line 120 why is it giving me the 120 at the end? Thanks

##### Share on other sites
Well, you're calculating the factorial not the fibonnacci series. 5! is 120.

So if you're wondering why you're not getting the correct fib number that is why. If you want to know why the code works look at the factorial definition:

1*2*3*...*N= N!

Let's say you know the factorial of N-1, then all you need to do is multiply by N. How do you find N-1? Find N-2. And so on until you get to 1. If you wish to know how to calculate the fibonacci series then that's a whole other ball of wax.

EDIT: Okay, oops. I finally understand your question, I'm sorry. Maybe fib is printing the final return value?

##### Share on other sites
if I put in return 0 instead I get a zero at the end.

##### Share on other sites
Most python IDE's will print the return-value of any function-call you make from the command-line, which is why you get the final '120' printed.

However, as nobodynews pointed out, your function calculates the factorial of n rather that the fibonacci number (which the name of the function seems to imply).

A function for calculating fib might be:
def fib(n):    if ( n == 1 ):        return 1;    if ( n == 2 ):        return 1;    return fib(n-2) + fib(n-1);

- Neophyte

##### Share on other sites
I think what he's asking is why it gets printed after the "End of the line". This is rather puzzling, indeed. I say a fork in your computer.

##### Share on other sites
My main concern is why it returns 120 instead of 1.

##### Share on other sites
fib(1) returns 1, but you aren't calling fib(1) from the IDE. The return value of fib(5) is 120, which is why the 120 is being printed.

##### Share on other sites
Then what is going on when I replace "return 1" with "return 0" and the result come out always zero; is the program returning the value * (the return value)

I know it strange to be concern with this detail, but I feel not knowing this information could lead to my programs not doing what I believe they should do.

##### Share on other sites
Quote:
 Original post by ZarisThen what is going on when I replace "return 1" with "return 0" and the result come out always zero; is the program returning the value * (the return value)I know it strange to be concern with this detail, but I feel not knowing this information could lead to my programs not doing what I believe they should do.

fib(which, actually (as others have said) is returning a factorial, not a fibonacci number) calls itself:
fib(5) = (5 * fib(4))       = (5 * (4 * fib(3)))       = (5 * (4 * (3 * fib(2))))       = (5 * (4 * (3 * (2 * fib(1)))))       = (5 * (4 * (3 * (2 * 1))))       = 120

So, if the return 1 was replaced with a return 0, the result would be (5 * (4 * (3 * (2 * 0)))), which is 0.

##### Share on other sites
Wow, I've never seem so many people failing to read a post before responding... [headshake]

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 9
• 9
• 34
• 16
• 11
• ### Forum Statistics

• Total Topics
634123
• Total Posts
3015651
×