我有一個用戶猜測字母的遊戲。他們被顯示爲神祕作品的空白版本(例如,_____
,_等於單詞中的字符數)。該程序知道該單詞,並且如果他們猜測的字母出現在神祕單詞中,則需要替換該單詞的空白版本中的每個索引。替換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