<div dir="ltr">Thank you guys</div><div class="gmail_extra"><br><div class="gmail_quote">On Thu, Nov 6, 2014 at 3:39 AM, Dave Angel <span dir="ltr"><<a href="mailto:davea@davea.name" target="_blank">davea@davea.name</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">William Becerra <<a href="mailto:wbecerra1@gmail.com">wbecerra1@gmail.com</a>> Wrote in message:<br>
<div><div class="h5">><br>
<br>
have the following code:<br>
names = "John, Cindy, Peter"<br>
def find(str, ch, s):<br>
    index = 0<br>
    while index < len(str):<br>
        if s==1:<br>
            for char in names[:4]:<br>
                if str[index] == ch:<br>
                    return index + 1<br>
                index = index + 1<br>
        if s==2:<br>
            for char in names[6:11]:<br>
                if str[index] == ch:<br>
                    return index + 1<br>
                index = index + 1<br>
        if s==3:<br>
            for char in names[13:]:<br>
                if str[index] == ch:<br>
                    return index + 1<br>
                index = index + 1<br>
    return -1<br>
print find(names,"n", 2)<br>
<br>
<br>
<br>
and my problem is:<br>
I intend for the parameter s to tell the interpreter which name to<br>
 look at<br>
so that i get the index return value related to that name.<br>
for example:<br>
John and Cindy both have a letter 'n' but when I call the function<br>
with an s value of 2 I want it to return the index value of the<br>
 letter n in Cindy and not in John.<br>
<br>
</div></div>Your most immediate problem is that you're using index to fetch<br>
 characters from the original string, and that's only reasonable<br>
 for John. You could make a slice of the original, and search that<br>
 slice. Or you could change the comparison to:<br>
   if char == ch:<br>
<br>
The loop could also be cleaner with enumerate.<br>
<br>
Your second big problem is that you've hard coded the sizes of the<br>
 first two names in the slice expressions. The magic [6:11] for<br>
 example  certainly doesn't belong inside the function.<br>
<br>
<br>
Third is your while loop makes no sense. Each of the if clauses<br>
 won't finish till you're done with the selected substring,  so<br>
 you might as well return right away. Drop that line entirely.<br>
<br>
<br>
Anyway, I say you're trying to do too much in the one function. If<br>
 possible change the data structure of names to eliminate one of<br>
 the two tasks, or write two functions, a few lines<br>
 each.<br>
<br>
I'd make<br>
  names = ["John", "Cindy", "Peter"]<br>
And start the function with<br>
  name = names [s-1]<br>
<br>
And now the task of the remainder of the function is to search a<br>
 name for a particular character.<br>
<span class="HOEnZb"><font color="#888888"><br>
--<br>
DaveA<br>
</font></span><div class="HOEnZb"><div class="h5"><br>
_______________________________________________<br>
Tutor maillist  -  <a href="mailto:Tutor@python.org">Tutor@python.org</a><br>
To unsubscribe or change subscription options:<br>
<a href="https://mail.python.org/mailman/listinfo/tutor" target="_blank">https://mail.python.org/mailman/listinfo/tutor</a><br>
</div></div></blockquote></div><br></div>