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 に答える