print("Welcome to Hangman! Guess the mystery word with less than 6 mistakes!")
words= ['utopian','fairy','tree','monday','blue']
i=int(input("Please enter a number (0<=number<10) to choose the word in the list: "))
if(words[i]):
print("The length of the word is: " , len(words[i]))
guesses=0
while guesses<6:
guess=input("Please enter the letter you guess: ")
if(guess in words[i]):
print("The letter is in the word.")
else:
print("The letter is not in the word.")
guesses=guesses+1
if guesses==6:
print("Failure. The word was:" , words[i])
在神秘单词中查找猜测字母的位置时遇到问题。我想要一个输出来显示神秘单词中正确猜测的字母。 前任。神秘词是蓝色的。用户输入“b”。输出为:“该字母在单词中。匹配的字母:b___”
最佳答案
有很多种写法,如果将所有猜测的字母放入名为 guessed
的字符串中,然后计算
''.join(c if c in guessed else '_' for c in words[i])
c 将迭代要猜测的单词 words[i]
的字符。
猜测 else '_' 中的 c if c
位将所有尚未猜测的字符替换为下划线。
''.join()
会将字符重新粘在一起形成字符串。
关于python - python 3.x中的Hangman,找到 secret 单词中字母的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19528512/