[Tutor] FW: find() problem

Roelof Wobben rwobben at hotmail.com
Tue Aug 24 16:23:03 CEST 2010



 


From: rwobben at hotmail.com
To: oberoc at gmail.com
Subject: RE: [Tutor] find() problem
Date: Tue, 24 Aug 2010 12:25:24 +0000





 
> Date: Tue, 24 Aug 2010 08:09:54 -0400
> Subject: Re: [Tutor] find() problem
> From: oberoc at gmail.com
> To: rwobben at hotmail.com
> 
> > I will try again.
> >
> > def find(strng, ch, start, step=1):
> >     index = start
> The problem lies here, if you do a print on index, it never gets past
> the first index of the number, so
> your while loop below goes into an infinite loop.
> 
> For example:
> find('starfruit','t',0) <- First time will return 1
> find('starfruit','t',1) <- Still returns 1. Infinite loop
> 
> >     while 0 <= index < len(strng):
> >         if strng[index] == ch:
> >             return index
> >         index += step
> >     return -1
> >
> > fruit=""
> > letter=""
> > fruit= raw_input("Enter a sort of fruit: ")
> > letter = raw_input("Enter the character which must be counted: ")
> > start=0
> > aantal=0
> > while 0 <=start < len(fruit):
> >     x=find (fruit,letter, start)
> >     aantal +=1
> >     start=x
> > print "De letter", letter , "komt", aantal , "maal voor in het woord", fruit
> >
> 
> HTH,
> Tino

Hello, 
 
Your right. index get never past 1 
But in my opinion when something is found then x will be the place where the character is be found.
After that the counter is increased and start gets the value of x.
So start should change.
 
Now finding out why this is not happening.
 
But thanks for the help.
 
Roelof
 
 		 	   		  
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20100824/a093fe97/attachment.html>


More information about the Tutor mailing list