[Tutor] trouble with list.remove() loop

D Ryan (2) dr8086 at bristol.ac.uk
Sat Sep 25 20:44:49 CEST 2010


Hello all,
I am currently trying to write a program which can find the solution to a
game of hangman.
In a part of the program, a user inputs a letter, a tester tells him if
the word contains that letter, and then if the answer is no, all words
containing that letter are removed from the list of remaining candidates.
However, the code I've written seems to remove some, but not all the words
containing the letter. Strangely though, if i run it a few more times it
gets some of the ones it missed the 1st time round, untill after enough
iterations it gets all of them. I cant understand why it doesnt remove all
of them the first time round. I have cut the offending code and formatted
it to work on its own, and also to fit into a relatively short email.

# A sample list of words, one of which is the answer.
candidates = ["abacus","amazing",
              "ozimandias",
              "a","alphanumeric",
              "functioning"]

# In the following code, the user has guessed the letter 'a',
# and the tester has told him that the letter 'a' is not in the word.

user_guess="a"
tester_response="no"

# The following code should eliminate all words which contain the letter
# 'a', leaving only the word 'functioning' in the list

if tester_response in ("No","no","n","N","NO"):
    for word in candidates:
        if user_guess in word:
            print word, "removed.."
            candidates.remove(word)
print candidates

Running once gives this output

abacus removed..
ozimandias removed..
alphanumeric removed..
['amazing', 'a', 'functioning']

But if i run it again it successfully removes 'amazing, and the next time
it removes 'a', leaving the correct answer. I'm perplexed by this strange
behaviour and would be most appreciative of any help. I'm very new to
python so apologies for any formatting/style errors, and also for the
simplicity of the problem.

Best regards
Dave



More information about the Tutor mailing list