[Tutor] find() problem

Roelof Wobben rwobben at hotmail.com
Tue Aug 24 10:47:46 CEST 2010


Hello, 

 

I have this exercise :

 

Now rewrite the count_letters function so that instead of traversing the string, it repeatedly calls find (the version from Optional parameters), with the optional third parameter to locate new occurences of the letter being counted.

 

 

And I have this solution :

 

'''
def find(strng, ch, start, step=1):
index = start
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

 

But it looks like it's in a indefinitive loop.

What went wrong here ?

 

I work with Python 2.7 on a Win7 machine.

 

Roelof

 
 		 	   		  
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20100824/e6116f3f/attachment.html>


More information about the Tutor mailing list