如何在相应的索引中仅打印单词中的猜测字母? [英] How can I print only guessed letter from a word in their corresponding indicies?

查看:36
本文介绍了如何在相应的索引中仅打印单词中的猜测字母?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个刽子手游戏,我需要制作一组与单词长度相同的下划线,当用户正确猜出一个字母时,下划线组中的相应空格就变成了正确猜出的字母.我该怎么做?

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?

推荐答案

你可以这样做:

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

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

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.

此示例的输出将是 "s____o____lo_".

这篇关于如何在相应的索引中仅打印单词中的猜测字母?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆