【问题标题】:Hangman written in python doesnt recognise correct letters guessed :/用 python 编写的 Hangman 无法识别猜测的正确字母:/
【发布时间】:2022-12-01 01:18:40
【问题描述】:

所以我正在做我的第一个项目,这个项目是用 python 编写的,每个项目都像视觉效果和不正确的字母一样工作。但是,它无法识别猜出的正确字母。

import random
from hangman_visual import lives_visual_dict
import string

# random word 

with open('random.txt', 'r') as f:
    All_Text = f.read()
    words = list(map(str, All_Text.split()))
WORD2GUESS = random.choice(words).upper()
letters = len(WORD2GUESS)
print("_" * letters)

word_letters = set(WORD2GUESS)
alphabet = set(string.ascii_uppercase)
lives = 7
used_letters = set()

# user input side 
while len(word_letters) > 0 and lives > 0:
        # letters used
        # ' '.join(['a', 'b', 'cd']) --> 'a b cd'
        print('You have', lives, 'lives left and you have used these letters: ', ' '.join(used_letters))

        # what current word is (ie W - R D)
        word_list = [letter if letter in used_letters else '-' for letter in WORD2GUESS]
        print(lives_visual_dict[lives])
        print('Current word: ', ' '.join(word_list))

        user_letter = input('Guess a letter: ').upper()
        if user_letter in alphabet - used_letters:
            used_letters.add(user_letter)
            if user_letter in WORD2GUESS:
                used_letters.remove(user_letter)
                print('')

            else:
                lives = lives - 1  # takes away a life if wrong
                print('\nYour letter,', user_letter, 'is not in the word.')

        elif user_letter in used_letters:
            print('\nYou have already used that letter. Guess another letter.')

        else:
            print('\nThat is not a valid letter.')

if lives == 0:
    print(lives_visual_dict[lives])
    print('You died, sorry. The word was', WORD2GUESS)
else:
    print('YAY! You guessed the word', WORD2GUESS, '!!')

我试过了,但它仍然无法识别正确的猜测。

        # what current word is (ie W - R D)
        word_list = [letter if letter in used_letters else '-' for letter in WORD2GUESS]
        print(lives_visual_dict[lives])
        print('Current word: ', ' '.join(word_list))

【问题讨论】:

    标签: python


    【解决方案1】:

    你的错误是这样的:当检查 user_letter 是否在 WORD2GUESS 中时,你错误地将它从 used_letters 中删除,但它必须留在集合中。

    在完全猜测单词时,您还缺少一些逃避 while 循环的方法。这可以通过检查 used_letters 是否包含 WORD2GUESS 中的所有字母来在同一地点完成。

    是这样的:

    import random
    from hangman_visual import lives_visual_dict
    import string
    
    # random word 
    
    with open('random.txt', 'r') as f:
        All_Text = f.read()
        words = list(map(str, All_Text.split()))
    WORD2GUESS = random.choice(words).upper()
    letters = len(WORD2GUESS)
    print("_" * letters)
    
    word_letters = set(WORD2GUESS)
    alphabet = set(string.ascii_uppercase)
    lives = 7
    used_letters = set()
    
    # user input side 
    while len(word_letters) > 0 and lives > 0:
            # letters used
            # ' '.join(['a', 'b', 'cd']) --> 'a b cd'
            print('You have', lives, 'lives left and you have used these letters: ', ' '.join(used_letters))
    
            # what current word is (ie W - R D)
            word_list = [letter if letter in used_letters else '-' for letter in WORD2GUESS]
            print(lives_visual_dict[lives])
            print('Current word: ', ' '.join(word_list))
    
            user_letter = input('Guess a letter: ').upper()
            if user_letter in alphabet - used_letters:
                used_letters.add(user_letter)
                if user_letter in WORD2GUESS:
                    print('
    That is a correct letter.')
                    if set(WORD2GUESS).issubset(used_letters):
                        break
    
                else:
                    lives = lives - 1  # takes away a life if wrong
                    print('
    Your letter,', user_letter, 'is not in the word.')
    
            elif user_letter in used_letters:
                print('
    You have already used that letter. Guess another letter.')
    
            else:
                print('
    That is not a valid letter.')
    
    if lives == 0:
        print(lives_visual_dict[lives])
        print('You died, sorry. The word was', WORD2GUESS)
    else:
        print('YAY! You guessed the word', WORD2GUESS, '!!')
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-07-17
      • 2012-09-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多