简体   繁体   English

如何在 Hangman 游戏中让一个猜测激活多个字母?

[英]How can I make one guess activate multiple letters in a Hangman game?

When I use a word with 2 of the same letter (like 'snazzy') a guess only activates 1 letter at a time.当我使用一个包含 2 个相同字母的单词时(例如 'snazzy'),猜测一次只能激活 1 个字母。 How can I fix this?我怎样才能解决这个问题?

l1=input('Input your (lowercase) letter:')
l2=input('Input your (lowercase) letter:')
l3=input('Input your (lowercase) letter:')
l4=input('Input your (lowercase) letter:')
l5=input('Input your (lowercase) letter:')
l6=input('Input your (lowercase) letter:')word=[l1,l2,l3,l4,l5,l6]
n1=''
n2=''
n3=''
n4=''
n5=''
n6=''
show=[n1,n2,n3,n4,n5,n6]
fail=0
good=0
while fail<=6 and good<6:
    guess=input('Guess a letter...')
    if guess in word:
        print('Right!')
        good=good+1
        if guess==l1:
            n1=guess
        elif guess==l2:
            n2=guess
        elif guess==l3:
            n3=guess
        elif guess==l4:
            n4=guess
        elif guess==l5:
            n5=guess
        elif guess==l6:
            n6=guess
        show=[n1,n2,n3,n4,n5,n6]
    else:
        print('No.')
        fail=fail+1
    print(show)
print(word)
if fail==7:
    print('Executioner wins!')
else:
    print('Prisoner wins!')

To clarify, I cannot guess the letter twice to show all of its instances.澄清一下,我无法猜测这封信两次以显示其所有实例。

Well, there are many things in your code that are not optimal, but here is a small improvement (which is also not optimal).好吧,您的代码中有很多不是最佳的,但这里有一个小的改进(这也不是最佳的)。 I used a for loop to look for all letters that match the guess.我使用 for 循环来查找与猜测匹配的所有字母。

num_letters = 6

word_to_guess = []
for _ in range(num_letters):
    word_to_guess.append(
        input('Input your (lowercase) letter:').lower().strip())

word_to_show = ['?', ] * num_letters
fail = 0
good = 0

while fail <= num_letters and good < num_letters:
    guess = input('Guess a letter...').lower().strip()
    if guess in word_to_guess:
        print('Right!')

        for i, letter in enumerate(word_to_guess):
            if guess == letter:
                good += 1
                word_to_show[i] = letter
    else:
        print('No.')
        fail += 1

    print(word_to_show)

print('word_to_guess', word_to_guess)
print('word_to_show', word_to_show)
if fail == 7:
    print('Executioner wins!')
else:
    print('Prisoner wins!')

Does that work for you?那对你有用吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM