Reputation: 27
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 set of underscores becomes the correctly guess letter. How can I do this?
userGuess = raw_input('Enter a letter or the word: ')
guessed = ''
def getWordList:
#just getting a word from a tct file and returning a random word from it
return word
def askForInput(userGuess):
xx = str(userGuess)
yy = xx.lower()
return yy
def showWord:
print'_ ' * len(word) #I know this part is wrong if I want to add the letters
print 'Guesses: %s' %guessed
if askForInput(userGuess) in word:
print 'There are %ss' %askForInput(userGuess).upper()
#now what can I do with showWord or how can I fix showWord?
Upvotes: 1
Views: 1881
Reputation: 82949
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_"
.
Upvotes: 2