How would I find all occurances of the letter 'e' for example in a list of the split word elephant?
I've tried using letterlist.index('e') for example but index only returns the first occurance of e.

This is to be used in a hangman game, and the game needs to display all the characters that have been guessed amongst a list of "-" dashes
So how would I replace "-" with the correctly guessed letter.


Edited by happygeek: changed from snippet to forum thread

6 Years
Discussion Span
Last Post by woooee
print(['e' if letter == 'e' else '-' for letter in 'elephant'])
['e', '-', 'e', '-', '-', '-', '-', '-']

Your code is not tested to work and does not belong to code snippets.

Edited by pyTony: indention does not work


So how would I replace "-" with the correctly guessed letter

There are 3 containers here, the "secret word", the hangman list, and the letter quessed. You would iterate over the "secret word" and compare letters. If the letters match then replace the "-" at the same postion in the hangman list..

for position in range(len(secret_word)):
    letter = secret_word[position]

Edited by woooee: None

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.