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_"
.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…