[Tutor] S.find()
Mark Lawrence
breamoreboy at yahoo.co.uk
Fri Jul 2 00:55:45 CEST 2010
On 01/07/2010 23:05, Corey Richardson wrote:
> Hello Tutors!
> I'm having a problem with the find() method of string objects. I'm
> currently making a hangman game, and I'm making the part that finds
> if there are multiple copies of the guessed letter in the word, and then
> if there are, finds them all. I can't for the life of me figure out the
> syntax of the find() method. gameWord = "python", btw.
>
> The module documentation lists it as this: "S.find(sub[, start[, end]])
> -> int".
What version of Python are you using? For Python 2.6.5 on Windows I
have from the compiled help file.
"
str.find(sub[, start[, end]])
Return the lowest index in the string where substring sub is found, such
that sub is contained in the range [start, end]. Optional arguments
start and end are interpreted as in slice notation. Return -1 if sub is
not found.
"
> I'm assuming sub is the string you want to find, and that is how it has
> worked out for me. (Bonus Points: What does sub mean? I'm guessing
See above.
> subscriptable, as one of my errors says, but I'll get to that...)
> When I try gameWord.find('p'[,1[,3]]), as the little help box suggests,
You don't need the square brackets, they're used in many forms of
documentation to indicate an optional argument.
> I get this:
>
> SyntaxError: invalid syntax
>
> Ok then, that is the exact syntax I was given. My next try is, and
> gives, this:
>
> >>> gameWord.find('p', [1,[3]])
>
> Traceback (most recent call last):
> File "<pyshell#99>", line 1, in <module>
> gameWord.find('p', [1,[3]])
> TypeError: slice indices must be integers or None or have an __index__
> method
>
>
> I assumed the comma after the 1 was messing it up, so I put this:
>
> >>> gameWord.find("p", [1[3]])
> Traceback (most recent call last):
> File "<pyshell#101>", line 1, in <module>
> gameWord.find("p", [1[3]])
> TypeError: 'int' object is not subscriptable
>
> Is subscriptable what sup stands for in find()? What does mean? (5 Bonus
> Points for answering that).
I'd prefer 5 bonus pints, but the documentation quoted above means I
couldn't really accept. Alright then, twist my arm if you must. :)
>
> I also tried passing a slice index right into it like gameWord.find('p',
> [1:4]), but that returned a SyntaxError as well.
>
> I have the entirety of my code posted up at
> http://pastebin.com/k9nMZNMy, I won't edit the code until I get this
> worked out, except maybe a few housekeeping things, documentation, etc.*
>
> *I've tried everything I can, and I appreciate your time and help!
>
> ~Corey Richardson
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
Kindest regards.
Mark Lawrence.
More information about the Tutor
mailing list