1

我正在制作一个刽子手游戏,我需要制作一组下划线,即单词的长度,当用户正确猜出一个字母时,下划线集中的相应空格将成为正确猜出的字母。我怎样才能做到这一点?

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?
4

1 回答 1

2

你可以这样做:

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

在这里,guess是一个字符串(或一个列表,或一个集合),包含用户到目前为止猜到的所有字母word,显然,是要猜测的单词。hintthen 是一个l包含单词中每个字母的列表,如果该字母在猜测的字母集中,则该字母或下划线。最后,将该提示连接到一个字符串并打印出来。

此示例的输出为"s____o____lo_".

于 2013-03-16T20:11:37.080 回答