variables in string.count
Peter Otten
__peter__ at web.de
Fri Jun 4 02:54:23 EDT 2004
ntg85 wrote:
> Can you use variables in string.count or string.find? I tried, and
> IDLE gives me errors.
> Here's the code:
I assume you omitted
import string
list = ["a", "b", "c"]
> while list:
> print
> letter = raw_input("What letter? ")
> string.lower(letter)
Strings are immutable, i. e. they cannot be modified. Therefore the above
should be:
letter = string.lower(letter)
or even better:
letter = letter.lower()
The string module is rarely needed these days as most functions are also
available as string methods.
> guess = string.count(letter, list)
To check if a string (letter) is in a list, use the "in" operator:
if letter in list:
list.remove(letter)
else:
print "Try again!"
> if guess == -1:
> print "Try again!"
> else:
> list.remove(letter)
> the error is:
> TypeError: expected a character buffer object
> Thanks.
Note that list is also a Python builtin and should not be used as a variable
name to avoid confusing the reader. For a complete list of builtins type
dir(__builtins__)
in the interpreter.
Peter
More information about the Python-list
mailing list