hi John<div>it's simple,</div><div>let's make a little modification for the scipt:</div><div><div>def fib(x):</div><div> if x==0 or x==1: return 1</div><div> else: return fib(x-1) + fib(x-2)</div><div><br></div>
<div>for i in range(10):</div><div> print 'fib(%s): ' % i, fib(i)</div><div><br></div><div>result:</div><div><div>fib(0): 1</div><div>fib(1): 1</div><div>fib(2): 2</div><div>fib(3): 3</div><div>fib(4): 5</div>
<div>fib(5): 8</div><div>fib(6): 13</div><div>fib(7): 21</div><div>fib(8): 34</div><div>fib(9): 55</div></div><div><br></div><div>you see, when we put 0 or 1 into fib, we definitely get 1 individually, if we put 2 into fib(), inside script we get two fib() result plus 1 + 1. If we put 9 into fib(), we get plus of two results of fib(7) and fib(8), that is 21 + 34, so we get 55.</div>
<div>hope it can give you a little help.</div><font class="Apple-style-span" face="song, Verdana"><span class="Apple-style-span" style="border-collapse: collapse; font-size: 12px; "><br></span></font><div class="gmail_quote">
On Thu, Aug 25, 2011 at 12:46 AM, John Gordon <span dir="ltr"><<a href="mailto:gordon@panix.com">gordon@panix.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
In <<a href="mailto:f99a156b-56b4-41f4-9599-0dcc31a47b0f@v9g2000pri.googlegroups.com">f99a156b-56b4-41f4-9599-0dcc31a47b0f@v9g2000pri.googlegroups.com</a>> <a href="mailto:kangshufan@hotmail.com">kangshufan@hotmail.com</a> writes:<br>
<br>
> Hi everyone<br>
<br>
> I just study python for a few time.<br>
> Now I have a problem and I holp someone can help me.<br>
> There is a piece of code:<br>
<br>
> def fib(x):<br>
> if x==0 or x==1: return 1<br>
> else: return fib(x-1) + fib(x-2)<br>
<br>
> Could you explain how it works?<br>
> Thanks for you help.<br>
<br>
> Vince<br>
<br>
When a function calls itself, as fib() does, it's called recursion.<br>
<br>
<a href="http://en.wikipedia.org/wiki/Recursion_(computer_science)" target="_blank">http://en.wikipedia.org/wiki/Recursion_(computer_science)</a><br>
<br>
Basically the fib() method keeps calling itself with smaller and smaller<br>
arguments until it gets 1 or 0.<br>
<br>
--<br>
John Gordon A is for Amy, who fell down the stairs<br>
<a href="mailto:gordon@panix.com">gordon@panix.com</a> B is for Basil, assaulted by bears<br>
-- Edward Gorey, "The Gashlycrumb Tinies"<br>
<font color="#888888"><br>
--<br>
<a href="http://mail.python.org/mailman/listinfo/python-list" target="_blank">http://mail.python.org/mailman/listinfo/python-list</a><br>
</font></blockquote></div><br></div>