[Tutor] Strings

William Becerra wbecerra1 at gmail.com
Wed Nov 5 14:19:13 CET 2014


Hey, I'm new to programming
running Python 2.7.8 Windows 8.1
I was reading 'How to Think Like a Computer Scientist- Learning with Python'
chapter 7 sub-chapter 7.7

I have the following code:
names = "John, Cindy, Peter"
def find(str, ch, s):
    index = 0
    while index < len(str):
        if s==1:
            for char in names[:4]:
                if str[index] == ch:
                    return index + 1
                index = index + 1
        if s==2:
            for char in names[6:11]:
                if str[index] == ch:
                    return index + 1
                index = index + 1
        if s==3:
            for char in names[13:]:
                if str[index] == ch:
                    return index + 1
                index = index + 1
    return -1
print find(names,"n", 2)



and my problem is:
I intend for the parameter s to tell the interpreter which name to look at
so that i get the index return value related to that name.
for example:
John and Cindy both have a letter 'n' but when I call the function
with an s value of 2 I want it to return the index value of the letter n in
Cindy and not in John.

Can Someone Please tell me why my code isn't working like I intend it to?
Thank you
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20141105/073881a0/attachment.html>


More information about the Tutor mailing list