如何用字母hangman替换dash

2024-06-06 22:13:22 发布

您现在位置:Python中文网/ 问答频道 /正文

我正试着为班级制作一个刽子手游戏,我被困在如何用正确猜出的字母替换破折号(-)上

比如,如果单词是HAPPY,用户猜到了字母p,那么它将替换字母破折号,如下所示:--PP-

以下是我目前的代码:

def play_game(secret_word):
guesses_left = 8
hangman_dash = len(secret_word) * "-"
while guesses_left > 0:
    print("The word now looks like this: " + (hangman_dash))
    print("You have " + str(guesses_left) + " guesses left")
    letter = input("Type a single letter, then press enter: ")
    letter = letter.upper()
    if len(letter) != 1:
        print("Please enter only one letter.")
    elif letter not in 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWYXZ':
        print("Please guess a letter.")
    if letter in secret_word:
        print("That guess is correct")

    else:
        print("There are no {}'s in the word".format(letter))
        guesses_left = guesses_left - 1

Tags: insecretlenif字母leftwordprint
3条回答

我已经扩展了红线提供的代码。它还检查字母字符

def hangman(word):
    hidden_word = "-" * len(word)
    print("This is the hidden word " + hidden_word)
    while True:
        user_input = input("Guess a letter: ")
        if user_input.isalpha():
            if user_input in word:
                index = word.find(user_input)
                hidden_word = hidden_word[:index] + user_input + hidden_word[index + 1:]
                print(hidden_word)
            else:
                print("Sorry that letter was not found, please try again.")
        else:
            print("Please use letters in the alphabet.")

hangman("word")

根据要求,我添加了一个包含两个相同字母的单词示例。我希望这有帮助

def hangman(secret_word):
    hangman_dash = len(secret_word) * "-"
    while True:
        user_input = input("Guess:")
        index = 0
        for char in secret_word:
            if char == user_input:
                hangman_dash = hangman_dash[:index] + char + hangman_dash[index + 1:]
                print(hangman_dash)
            index += 1

hangman("happy")

我为此创建了一个非常简单的示例。本质上if user_input in word然后我们找到用户输入的索引,并用隐藏字母替换它。代码写得不是很好:),但它可以做到这一点

def hangman(word):
    # Replace word with dashes    
    hidden_word = "-" * len(word)
    print("This is the hidden word " + hidden_word)
    # Get user's guess
    user_input = input("Guess a letter: ")
    # If the user's guess exists in the string
    if user_input in word:
        # Find all occurences of user's guess in word
        occurences = findOccurrences(word, user_input)
        # For each occurenc, replace that dash in the string with the correct letter
        for index in occurences:
            hidden_word = hidden_word[:index] + user_input + hidden_word[index + 1:]
        # Return the updated hidden_word
        print(hidden_word)
    # If the user's guess isn't in the string
    else:
        user_input = input("Sorry that letter was not found, please try again: ")

# Find all occurences method
def findOccurrences(s, ch):
    return [i for i, letter in enumerate(s) if letter == ch]

hangman("hello")nput("Guess a letter: ")
    if user_input in word:
        occurences = findOccurrences(word, user_input)
        for index in occurences:
            hidden_word = hidden_word[:index] + user_input + hidden_word[index + 1:]
        print(hidden_word)
    else:
        user_input = input("Sorry that letter was not found, please try again: ")

def findOccurrences(s, ch):
    return [i for i, letter in enumerate(s) if letter == ch]

hangman("hello")

遍历字符串不值得编程。只需在循环的每次迭代中将hangman_dash变量设置为'',并创建一个类似下面的for循环,以检查每个字母是否正确:

def play_game(secret_word):
guesses_left = 8
while guesses_left > 0:
    hangman_dash=''
    for i in secret_word:
        if i in good_guesses:
            hangman_dash+=i
        else:
            hangman_dash+='-'
    print("The word now looks like this: " + (hangman_dash))
...

因此,如果猜到了secret_word中的一个字母(或在good_guesses字符串中),那么无论该字母是什么,都将添加到hangman_dash。否则,将添加'-'

相关问题 更多 >