<html>
<head>
<style><!--
.hmmessage P
{
margin:0px;
padding:0px
}
body.hmmessage
{
font-size: 12pt;
font-family:Calibri
}
--></style></head>
<body class='hmmessage'><div dir='ltr'>Hi<div><br></div><div>Shouldn't your code be like this</div><div><br></div><div><div>def fib(n):</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>if n==0:</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>return 0</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>else:</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>return n + fib(n-1)</div><div><br></div><div>this works</div><div><br></div><div><div>>>> for i in range(4):</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>print fib(i)</div><div><br></div><div><span class="Apple-tab-span" style="white-space:pre"> </span></div><div>0</div><div>1</div><div>3</div><div>6</div><div>>>> </div></div><br><div>> To: tutor@python.org<br>> From: davea@davea.name<br>> Date: Thu, 6 Feb 2014 18:06:41 -0500<br>> Subject: Re: [Tutor] learning recursion<br>> <br>> Denis Heidtmann <denis.heidtmann@gmail.com> Wrote in message:<br>> ><br>> > <br>> Please post in text, not html. Your posting program loses the<br>> indentation in the text view, which is what most people<br>> see.<br>> <br>> Code:<br>> def fib2(n):<br>> if n==1:<br>> return 1<br>> <br>> elif n==2:<br>> return 1<br>> else:<br>> return fib2(n-2) +fib2(n-1)<br>> <br>> That code doesn't do anything reasonable for zero or negative<br>> values. Probably what you want is<br>> <br>> if n==0:<br>> return 0<br>> elif n <3:<br>> return 1<br>> else:<br>> <br>> It would probably be technically correct to raise an exception for<br>> n < 1, because fibonacci didn't define earlier values. But it's<br>> apparently acceptable to return zero for the zero<br>> case.<br>> <br>> <br>> <br>> <br>> -- <br>> DaveA<br>> <br>> _______________________________________________<br>> Tutor maillist - Tutor@python.org<br>> To unsubscribe or change subscription options:<br>> https://mail.python.org/mailman/listinfo/tutor<br></div></div> </div></body>
</html>