Python 我怎样才能只打印单词对应标记中的猜测字母?
我正在制作一个刽子手游戏,我需要制作一组下划线,即单词的长度,当用户正确猜出一个字母时,下划线集中的相应空格将成为正确猜出的字母。我该怎么做Python 我怎样才能只打印单词对应标记中的猜测字母?,python,python-2.7,Python,Python 2.7,我正在制作一个刽子手游戏,我需要制作一组下划线,即单词的长度,当用户正确猜出一个字母时,下划线集中的相应空格将成为正确猜出的字母。我该怎么做 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 wor
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
,或者是该字母(如果该字母在猜测字母集中)或者下划线。最后,该提示被连接到一个字符串并打印出来
这个例子的输出是
“s_uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu?还有,你能想出一个更好的问题标题吗?@MartijnPieters,问题在最后一条评论行我知道我可以做一些类似的列表和元组赋值(我想这就是它的名字),但我不希望在运行代码时tp有方形的brakcets。你不需要中间列表提示
。只需使用print'.join(如果我在猜猜中是l,那么在word中是l)
@martineau当然,那确实也是我的第一个版本,但我决定把它分开,一次解释一件事。