Skip to content Skip to sidebar Skip to footer

How Can I Print Only Guessed Letter From A Word In Their Corresponding Indicies?

I am making a hangman game and I need to have to make a set of underscores that is the length of the word and when the user correctly guess a letter the corresponding space in the

Solution 1:

You could do something like this:

guess = "sol"
word = "stackoverflow"
hint = [l if l in guess else"_" for l in word]
print "".join(hint)

Here, guess is a string (or a list, or a set) holding all the letters the user has guessed so far, and word, obviously, is the word to guess. hint then is a list holding for each letter l in the word either that letter, if it is in the set of guessed letters, or an underscore. Finally, that hint is joined to a string and printed.

Output for this example would be "s____o____lo_".

Post a Comment for "How Can I Print Only Guessed Letter From A Word In Their Corresponding Indicies?"