there is a problem, holp someone could help me,thanks

K

kangshufan

Hi everyone

I just study python for a few time.
Now I have a problem and I holp someone can help me.
There is a piece of code:

def fib(x):
if x==0 or x==1: return 1
else: return fib(x-1) + fib(x-2)

Could you explain how it works?
Thanks for you help.

Vince
 
J

John Gordon

In said:
Hi everyone
I just study python for a few time.
Now I have a problem and I holp someone can help me.
There is a piece of code:
def fib(x):
if x==0 or x==1: return 1
else: return fib(x-1) + fib(x-2)
Could you explain how it works?
Thanks for you help.

When a function calls itself, as fib() does, it's called recursion.

http://en.wikipedia.org/wiki/Recursion_(computer_science)

Basically the fib() method keeps calling itself with smaller and smaller
arguments until it gets 1 or 0.
 
R

rantingrick

Now I have a problem and I holp someone can help me.

def fib(x):
    if x==0 or x==1: return 1
    else: return fib(x-1) + fib(x-2)

This must be from "How not to program". Was this a personal pick or
recommendation?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top