试图弄清楚of子手的游戏

时间:2019-06-25 01:18:11

标签: python

我是一个极端的初学者,目前正在尝试使用Python 3创建一个hangman游戏。

貌似,我遇到的唯一问题是用户输入字符时,我不太确定如何用用户输入的正确字母替换“ _”。

import random


def hangman():
    # setting up the word and spacing
    possible_words = ["SOFTWARE", "COMPUTER", "RESEARCH", "BLIZZARD"]
    chosen_word = random.choice(possible_words)
    unknown_word = chosen_word
    # del
    print(chosen_word)
    # del
    for char in unknown_word:
        unknown_word = unknown_word.replace(char, "_ ")
    print(unknown_word)
    y = list(unknown_word)
    print()

    # Playing the game
    mistakes = 0
    incorrect_guesses = []
    while mistakes < 5:
        guess = input("Enter a guess:\n")
        length = len(guess)
        # PROBLEMS
        if guess.upper() in chosen_word and length == 1:
            print(unknown_word)
            print()
        # PROBLEMS
        elif length != 1:  # For if more than 2 characters are entered.
            print("Please enter a single character.")
            print()
            continue
        else:  # If the guess isn't in the string
            print("Incorrect guess!")
            mistakes += 1
            print("You have {0} attempts left".format(5 - mistakes))
            incorrect_guesses.append(guess)
            print("You have used {0} so far.".format(incorrect_guesses))
            print()
    print("GAME OVER")


hangman()

因此,我需要弄清楚的问题是,如果用户输入正确的字符会发生什么情况。

提前谢谢!

3 个答案:

答案 0 :(得分:1)

首先,我建议为set()使用list而不是incorrect_guesses(您将需要使用.add而不是.append) 。这样,如果用户重复相同的字符,您将避免使用重复的值。

您还可以使用一个correct_guesses变量来跟踪成功的尝试,并避免在_中用unknown_word替换它们。

在此更改之后,受影响的代码如下:

    incorrect_guesses = set()
    correct_guesses = set()
    while mistakes < 5:
        guess = input("Enter a guess:\n")
        length = len(guess)
        char = guess.upper()
        if char in chosen_word and length == 1:
            correct_guesses.add(char)
            unknown_word = chosen_word
            for char in (c for c in unknown_word if c not in correct_guesses):
                unknown_word = unknown_word.replace(char, "_ ")
            print(unknown_word)

答案 1 :(得分:0)

下面的代码将用原始字符替换_

unknown_word = ' '.join([guess.upper() if chosen_word[i] == \
                guess.upper() else c for i, c in enumerate(
                 unknown_word.replace(' ', ''))]
                ) + ' '

答案 2 :(得分:0)

您可以做类似...

unknown_word = chosen_word #define the word
current_guess = '_'*len(unknown_word) #get the blanks (if you keep the spaces the loc parameter below will be multiplied by 2)
guessed_letter = input("Enter a guess:\n") #get the guessed letter

if guessed_letter in unknown_word:
    loc=unknown_word.index(guessed_letter)
    current_guess=current_guess[:loc]+guessed_letter+current_guess[loc+1:]

如果在unknown_word中,它将用相应位置的字母替换空白

正如所提到的那样。如果保留空格,索引位置(位置)将乘以2

编辑-以上仅对每个字母出现一次...这将适用于多次出现相同字母的单词:

guessed_letter = input("Enter a guess:\n")
for loc in range(len(unknown_word)):
    if unknown_word[loc] == guessed_letter:
        current_guess = current_guess[:loc]+guessed_letter+current_guess[loc+1:]
相关问题