Connecting Tech Pros Worldwide Forums | Help | Site Map

variables in string.count

ntg85
Guest
 
Posts: n/a
#1: Jul 18 '05
Can you use variables in string.count or string.find? I tried, and
IDLE gives me errors.
Here's the code:
while list:
print
letter = raw_input("What letter? ")
string.lower(letter)
guess = string.count(letter, list)
if guess == -1:
print "Try again!"
else:
list.remove(letter)
the error is:
TypeError: expected a character buffer object
Thanks.

Peter Otten
Guest
 
Posts: n/a
#2: Jul 18 '05

re: variables in string.count


ntg85 wrote:
[color=blue]
> Can you use variables in string.count or string.find? I tried, and
> IDLE gives me errors.
> Here's the code:[/color]

I assume you omitted

import string
list = ["a", "b", "c"]
[color=blue]
> while list:
> print
> letter = raw_input("What letter? ")
> string.lower(letter)[/color]

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.
[color=blue]
> guess = string.count(letter, list)[/color]

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!"
[color=blue]
> if guess == -1:
> print "Try again!"
> else:
> list.remove(letter)
> the error is:
> TypeError: expected a character buffer object
> Thanks.[/color]

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
Matteo Dell'Amico
Guest
 
Posts: n/a
#3: Jul 18 '05

re: variables in string.count


ntg85 wrote:[color=blue]
> Can you use variables in string.count or string.find? I tried, and
> IDLE gives me errors.
> Here's the code:
> while list:
> print
> letter = raw_input("What letter? ")
> string.lower(letter)
> guess = string.count(letter, list)
> if guess == -1:
> print "Try again!"
> else:
> list.remove(letter)
> the error is:
> TypeError: expected a character buffer object
> Thanks.[/color]

You shouldn't use list as a variable name: it's already a builtin
function, and it could create problems to you.

As your exception states, string.count needs a string to work on, and
string.lower doesn't modify the object in place (strings are immutable),
but it would return a new object.

I'd code it like this:

while secret:
print
letter = raw_input("What letter? ").lower()
# we probably should do something to make sure len(letter) == 1
if letter not in secret:
print "Try again!"
else:
secret = secret.replace(letter, '', 1)

--
Ciao,
Matteo
Scott David Daniels
Guest
 
Posts: n/a
#4: Jul 18 '05

re: variables in string.count


Matteo Dell'Amico wrote:
.... (good stuff)...[color=blue]
> I'd code it like this:
>
> while secret:
> print
> letter = raw_input("What letter? ").lower()
> # we probably should do something to make sure len(letter) == 1
> if letter not in secret:
> print "Try again!"
> else:
> secret = secret.replace(letter, '', 1)[/color]

Exceptions are your friend, don't ask permission:

def tests(secret):
parts = list(secret.lower())
while parts:
reply = raw_input("What letter(s)? ")
for letter in reply:
try:
parts.remove(letter.lower())
except ValueError:
print "%r not found" % letter
if parts:
print len(parts), 'letters left; try again.'


--
-Scott David Daniels
Scott.Daniels@Acm.Org
Closed Thread