[Tutor] FW: find() problem

Evert Rol evert.rol at gmail.com
Tue Aug 24 18:42:57 CEST 2010


> I found it.

Good.
Few generic comments nonetheless, just for the fun of it ;-).

> This one does the trick :
>  
> def find(strng, ch, start, step=1):
>     index=start
>     while 0 <= index < len(strng):
>         if strng[index] == ch:
>               return index 
>         index += step
>     return -2

You can actually use 'return index'. When you reach the end of the function, index == len(strng), which would be invalid in your main while condition, and the +1 would still keep it that way.
But read on if you want to keep the return -1 from the book.

> 
> fruit=""
> letter=""
> fruit= raw_input("Enter a sort of fruit: ")
> letter = raw_input("Enter the character which must be counted: ")
> index=1
> aantal=0
> while 0 <=index < len(fruit):
>     x=find (fruit,letter, index)

You can check for x == -1 here (if you have the old find() from the book), and then break out of the loop. Provided the book has already dealt with the break statement. If you use break, your while condition can simply be 'while True:'

>     index=x+1
>     if x<>-2 :

Don't use <>, but != instead. 
If you ever use Python 3, it doesn't work anymore: http://docs.python.org/release/3.0.1/whatsnew/3.0.html#removed-syntax


>         aantal=aantal+1
> print "De letter", letter , "komt", aantal , "maal voor in het woord", fruit
>  
> Roelof
> 
> > 
> > > > > > > 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
> > 
> > Why are you returning -1 here? 
> > -1 is a valid list index. 
> > And with your last change, it makes your main condition valid.
> > 
> > 
> > > > > > >
> > > > > > > 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.
> > > > 
> > > > You're including the previous found index; so it will just find the character at that same index again. You need to start the next search one index further down the word.
> > > 
> > > 
> > > Hello Evert.
> > > 
> > > Stupid mistake.
> > > I change start=x to start=x+1 
> > > But still it's not working properly.
> > > 
> > > I can see that on this example.
> > > 
> > > Fruit : banaan
> > > letter : a
> > > 
> > > That a is being found at 2,4,5 which is correct but the programm don't end.
> > > So there's another error somewhere,
> > > 
> > > Roelof
> > > 


More information about the Tutor mailing list