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])
在神秘词中找到猜测字母的位置时遇到问题。我想要一个输出,显示神秘词中正确猜到的字母。 防爆。神秘的词是蓝色的。用户输入&#34; b&#34;。输出是:&#34;字母在单词中。匹配的字母:b ___&#34;
答案 0 :(得分:1)
有很多方法可以写这个,如果你把所有猜到的字母放在名为guessed
的字符串中,然后计算
''.join(c if c in guessed else '_' for c in words[i])
c将迭代words[i]
的字符,即要猜的字。
c if c in guessed else '_'
位替换所有尚未使用下划线猜测的字符。
''.join()
会将字符粘合成一个字符串。
答案 1 :(得分:0)
我认为你正在寻找这样的东西:
word = []
for x in range(len(words[i])):
word.append('_')
if(guess in words[i]):
print("The letter is in the word.")
for index, letter in enumerate(words[i]):
if letter == guess:
word[index] = guess
一个完整的程序,如:
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
letters_guessed = []
word = []
for x in range(len(words[i])):
word.append('_')
while guesses < 6:
guess=input("Please enter the letter you guess: ")
if(guess in words[i]):
print("The letter is in the word.")
for index, letter in enumerate(words[i]):
if letter == guess:
word[index] = guess
letters_guessed.append(guess)
else:
print("The letter is not in the word.")
guesses=guesses+1
letters_guessed.append(guess)
print("you have guessed these letters: %s"%(''.join(letters_guessed)))
print("Letters matched so far %s"%''.join(word))
print()
if ''.join(word) == words[i]:
break
if guesses==6:
print("Failure. The word was:" , words[i])
else:
print("YOU'VE WON!! Great Job!")
print("You only made %i wrong guesses"%guesses)
输出:
> Welcome to Hangman! Guess the mystery word with less than 6 mistakes!
> Please enter a number (0<=number<10) to choose the word in the list: 2
> The length of the word is: 4
> Please enter the letter you guess: e
> The letter is in the word.
> you have guessed these letters: e
> Letters matched so far __ee
答案 2 :(得分:0)
上面的代码有效,但如果您输入的数字高于5,则只有5个项目可供选择,因此我在列表中添加了其他一些单词,但我也删除了带有输入功能的行,其中要求用户数字与要发现的单词相关的项目的编号。 所以: 我导入了随机模块 我删除了输入代码 我使用random.sample函数存储要发现的单词(第12行) 我用samplecode [0]代替单词[i]作为要发现的单词的标签 我在word.append('_')中的下划线后面添加了一个空格,使字母数更明显(_ _ _ _ _而不是_____)。
import random
print("Welcome to Hangman! Guess the word in less than 6 try.")
words= ['utopian','fairy','tree','monday','blue',
'winner','chosen','magician','european',
'basilar','fonsaken','butter','butterfly',
'flipper','seaside','meaning','gorgeous',
'thunder','keyboard','pilgrim','housewife'
]
sampleword = random.sample(words,1)
if(sampleword[0]):
print("The length of the word is: " , len(sampleword[0]))
guesses=0
letters_guessed = []
word = []
for x in range(len(sampleword[0])):
word.append('_ ')
while guesses < 6:
guess=input("Please enter the letter you guess: ")
if(guess in sampleword[0]):
print("The letter is in the word.")
for index, letter in enumerate(sampleword[0]):
if letter == guess:
word[index] = guess
letters_guessed.append(guess)
else:
print("The letter is not in the word.")
guesses=guesses+1
letters_guessed.append(guess)
print("you have guessed these letters: %s"%(''.join(letters_guessed)))
print("Letters matched so far %s"%''.join(word))
print()
if ''.join(word) == sampleword[0]:
break
if guesses==6:
print("Failure. The word was:" , sampleword[0])
else:
print("YOU'VE WON!! Great Job!")
print("You only made %i wrong guesses"%guesses)