下面是我的功能中有问题的部分:
def hangman1(word):
global guessesMade
global guessesLeft
currentGuess = '_ ' * len(word)
let = print(input('Please guess a letter: '))
for i in range(len(word)):
if word[i] == let:
print('{} is contained in the word.'.format(let))
if i == 0:
currentGuess = word[0] + currentGuess[1:]
else:
currentGuess = currentGuess[:i] + word[i] + currentGuess[i + 1:]
print(currentGuess)用户在提示符下输入一个字母,并检查该字母是否在函数外部从单词列表生成的randomWord中。我可以让它正确地打印空格,但是如果用户输入单词中的一个字母,它就打印出一行正确的字母,而不是中间混合正确字母的空格。
任何帮助都是非常感谢的。
发布于 2014-10-29 03:08:52
hiddenLet.replace('_',let)用_所代表的任何内容替换_的所有出现。
newWordList = [x if x==let else '_' for x in randWord]
newWord = ''.join(newWordList)https://stackoverflow.com/questions/26621971
复制相似问题