我有一个用户猜测字母的游戏。他们被显示为神秘作品的空白版本(例如,_____
,_等于单词中的字符数)。该程序知道该单词,并且如果他们猜测的字母出现在神秘单词中,则需要替换该单词的空白版本中的每个索引。替换Python中字符的每个实例字符串
例如,如果玩家猜测“p”,单词是“河马”,他们将显示__pp_
。但是,我的代码只会替换“p”的第一个实例,代之以__p__
。
这会更容易处理列表问题吗?
mistakes = 0
complete = False
t = False
words = ['cow','horse','deer','elephant','lion','tiger','baboon','donkey','fox','giraffe']
print("\nWelcome to Hangman! Guess the mystery word with less than 6 mistakes!")
# Process to select word
word_num = valid_number()
word = words[word_num]
#print(word)
print("\nThe length of the word is: ", str(len(word)))
attempt = len(word)*"_"
# Guesses
while not (mistakes == 6):
guess = valid_guess()
for letter in word:
if guess == letter:
print("The letter is in the word.")
position = word.index(guess)
attempt = attempt [0:position] + guess + attempt [position + 1:]
print("Letters matched so far: ", attempt)
t = True
while (t == False):
print("The letter is not in the word.")
print("Letters matched so far: ", attempt)
mistakes = mistakes + 1
hangMan = ["------------", "| |", "| O", "| / |", "| |", "| / |\n|\n|"]
hang_man()
t = True
t = False
这几乎和我在hang子手游戏中所做的完全一样。 – sayantankhan
不错:)我也是... –
谢谢,这个很容易理解!当我得到第二个时,我会沿着这些路线实施一些事情。 – user3290553