【发布时间】:2017-07-04 04:56:15
【问题描述】:
您好,我正在尝试在 python 上创建一个刽子手游戏, 当用户使用此方法输入正确的字母时,我能够显示隐藏的字母:
def getGuessedWord():
import re
guessed = re.sub(r'\S', '_', word)
l = checkLetterInWords()
if l == True:
new = ""
for index, char in enumerate(word):
if char == letter:
new += letter
else:
new += guessed[index]
guessed = new
print(guessed, "\n")
return guessed
但是,当我输入另一个正确的字母时,它不会显示它停止的位置。例如:
这个词是西瓜
输入:w
输出:w_ _ _ _ _ _ _ _ _
第二个输入:a
输出:_ a _ _ _ _ _ _ _ _
我如何使输入像
w _ _ _ _ _ _ _ _ ?
我在 stackoverflow 上查看了许多其他方法,但没有一个有效。有人可以帮我弄清楚吗?谢谢!
【问题讨论】:
-
不要在每次运行
getGuessedWord()函数时都导入re模块。 -
这段代码sn-p不完整。
word和checkLetterInWords()的定义是什么?为什么函数getGuessedWord没有参数?
标签: python python-3.5