[英]How to replace the repeat word in python (Hangman Game)!
This game now i have solved till the end.But here is 1 more problem that is how can i replace repeat word in string? 这个游戏现在我已经解决了直到最后。但是这里还有一个问题,那就是如何替换字符串中的重复单词? please help me! 请帮我! Thank you. 谢谢。 For example: my customfile inside there is word:"apple" and when hide the word with dashes: "-----" but when i replace in dashes string i can just only replace dash with 1 p:"ap-le" how can i replace dashes with 2p: "apple".My previous post for this program: How do i detect the repeat input in my hangman game (Python)! 例如:我的自定义文件里面有单词:“apple”,当用破折号隐藏单词时:“-----”但是当我用破折号替换字符串时我只能用1 p代替破折号:“ap-le”如何用2p代替破折号:“apple”。我之前的帖子对于这个程序: 我如何检测我的刽子手游戏(Python)中的重复输入! . 。 Here is my code to replace: 这是我要替换的代码:
def getGuessedWord():
pos = word.index(guessword.lower())
print(pos)
global words
words = words[:pos]+ guessword.lower() +words[pos+1:]
print(words)
return words
My solution to your overall problem of filtering out guessed letters is the following set of functions: 我对滤除猜测字母的整体问题的解决方案是以下一组函数:
guessed_letters = set()
def guess(letter):
global guessed_letters
guessed_letters.add(letter)
def filter_letter(letter):
if letter in guessed_letters:
return letter
else:
return '-'
def filtered(words):
for word in words:
output = ''
for letter in word:
output += filter_letter(letter)
yield output
or more compactly: 或者更紧凑:
guessed_letters = set()
def guess(letter):
global guessed_letters
guessed_letters.add(letter)
def filter_letter(letter):
return letter if letter in guessed_letters else '-'
def filtered(words):
return [''.join(map(filter_letter, word)) for word in words]
such that: 这样:
>>> words = "bubbly water".split()
>>> guess('a')
>>> ' '.join(filtered(words))
'------ -a---'
>>> guess('b')
>>> ' '.join(filtered(words))
'b-bb-- -a---
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.