variables in string.count
__peter__ at web.de
Fri Jun 4 08:54:23 CEST 2004
> 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
list = ["a", "b", "c"]
> while list:
> letter = raw_input("What letter? ")
Strings are immutable, i. e. they cannot be modified. Therefore the above
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:
print "Try again!"
> if guess == -1:
> print "Try again!"
> the error is:
> TypeError: expected a character buffer object
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
in the interpreter.
More information about the Python-list