variables in string.count

N

ntg85

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.
 
P

Peter Otten

ntg85 said:
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
 
M

Matteo Dell'Amico

ntg85 said:
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.

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)
 
S

Scott David Daniels

Matteo Dell'Amico wrote:
.... (good stuff)...
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)

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.'
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,169
Messages
2,570,920
Members
47,464
Latest member
Bobbylenly

Latest Threads

Top