[Tutor] recursive function

evros loizides loizie@hotmail.com
Sat May 31 14:39:02 2003


This is a multi-part message in MIME format.

------=_NextPart_000_0001_01C32779.CA15D3A0
Content-Type: text/plain;
	charset="us-ascii"
Content-Transfer-Encoding: 7bit

i need to use recursive fuction insted of while loop in the following
program .
 does anybody  have any good ideas how to do that?
 
 
 
#def isString()
 
import string
myString = raw_input("enter string:")
num=len(myString)
print myString
print len(myString)
n=1
sum=0
while n<=num:
    if myString[n-1].isdigit():
        #print "is digit"
        sum=sum+int(myString[n-1])
        #print sum
    #else:
        #print "is not digit"
        
    n=n+1    
print sum


------=_NextPart_000_0001_01C32779.CA15D3A0
Content-Type: text/html;
	charset="us-ascii"
Content-Transfer-Encoding: quoted-printable

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META HTTP-EQUIV=3D"Content-Type" CONTENT=3D"text/html; =
charset=3Dus-ascii">
<TITLE>Message</TITLE>

<META content=3D"MSHTML 6.00.2800.1170" name=3DGENERATOR></HEAD>
<BODY>
<DIV><FONT face=3DArial size=3D2><SPAN class=3D505083418-31052003>i need =
to use=20
recursive fuction insted of while loop in the following program=20
.</SPAN></FONT></DIV>
<DIV><FONT face=3DArial size=3D2><SPAN =
class=3D505083418-31052003>&nbsp;does=20
anybody&nbsp; have any good ideas how to do that?</SPAN></FONT></DIV>
<DIV><FONT face=3DArial size=3D2><SPAN=20
class=3D505083418-31052003></SPAN></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2><SPAN=20
class=3D505083418-31052003></SPAN></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2><SPAN=20
class=3D505083418-31052003></SPAN></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2><SPAN class=3D505083418-31052003>#def=20
isString()</SPAN></FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2><SPAN class=3D505083418-31052003>import =

string<BR>myString =3D raw_input("enter =
string:")<BR>num=3Dlen(myString)<BR>print=20
myString<BR>print len(myString)<BR>n=3D1<BR>sum=3D0<BR>while=20
n&lt;=3Dnum:<BR>&nbsp;&nbsp;&nbsp; if=20
myString[n-1].isdigit():<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
#print=20
"is digit"<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
sum=3Dsum+int(myString[n-1])<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp=
; #print=20
sum<BR>&nbsp;&nbsp;&nbsp; =
#else:<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
#print "is not digit"<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
<BR>&nbsp;&nbsp;&nbsp; n=3Dn+1&nbsp;&nbsp;&nbsp; <BR>print=20
sum<BR></SPAN></FONT></DIV></BODY></HTML>

------=_NextPart_000_0001_01C32779.CA15D3A0--