1
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___”

4

3 回答 3

1

有很多方法可以写这个,如果你把所有猜到的字母都放在被调用的字符串中guessed然后计算

''.join(c if c in guessed else '_' for c in words[i])

c 将遍历words[i]要猜测的单词 的字符。

c if c in guessed else '_'位用下划线替换所有尚未猜到的字符。

''.join()会将字符重新组合成一个字符串。

于 2013-10-22T22:39:56.807 回答
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
于 2013-10-23T00:26:15.813 回答
0

上面的代码有效,但是在单词列表中只有 5 个项目可供选择,如果输入大于 5 的数字,则可能会出错,因此我在列表中添加了一些其他单词,但我也删除了输入的行要求用户输入与要发现的单词相关的项目编号的功能。所以:我导入了随机模块。
我删除了输入代码。
我使用 random.sample 函数来存储要发现的单词(第 12 行)。
我将 words[i] 替换为 samplecode[0] 作为要发现的单词的标签。
我在 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)
于 2015-08-10T11:30:12.710 回答