python oddity

Started by
11 comments, last by Way Walker 18 years, 7 months ago
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
Advertisement
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?

C++: A Dialog | C++0x Features: Part1 (lambdas, auto, static_assert) , Part 2 (rvalue references) , Part 3 (decltype) | Write Games | Fix Your Timestep!

if I put in return 0 instead I get a zero at the end.
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
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.
I teleported home one night; With Ron and Sid and Meg; Ron stole Meggie's heart away; And I got Sydney's leg. <> I'm blogging, emo style
My main concern is why it returns 120 instead of 1.
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.
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.
Quote:Original post by Zaris
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.


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.
Wow, I've never seem so many people failing to read a post before responding... [headshake]
I teleported home one night; With Ron and Sid and Meg; Ron stole Meggie's heart away; And I got Sydney's leg. <> I'm blogging, emo style

This topic is closed to new replies.

Advertisement