游戏中的刽子手在玩完一轮后试图退出

2021-05-16 08:33:55 发布

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

如果我玩一轮游戏并选择退出选项,游戏结束,但是,如果我玩第二轮并尝试退出,游戏将继续,用户将被提示再次输入猜测,而不是终止游戏。在

它似乎陷入了一个循环。在

这是我的代码:

from random import randint
def core_game():

    def init_hangman():
        hangman = []

        for x in range(7):
            hangman.append([" "] * 7)
        hangman[0][0] = "_"
        hangman[0][1] = "_"
        hangman[0][2] = "_"
        hangman[1][3] = "|"
        return hangman

    hangman = init_hangman()

    def print_hangman():
        for x in hangman:
            print(str.join("", x))

    def get_input(guess):
            your_guess = input(guess)
            if your_guess in guessed_letters:
                print("You already guessed that letter!")
                return get_input(guess)
            elif your_guess.isalpha() and len(your_guess) == 1:
                return your_guess
            else:
                print("Please guess a single letter!")
                return get_input(guess)

    words_list = ["monkey", "cow"]
    city_list = ["Amarillo", "Houston"]
    lists = ["animals", "cities"]
    random_list = randint(0,1)
    random_word = randint(0,1)
    if lists[random_list] == "cities":
        rand_list = city_list
    elif lists[random_list] == "animals":
        rand_list = words_list
    word = rand_list[random_word]
    guessed = ""
    guessed_letters = []
    hang = 6
    Cont = True

    for letter in word:
        guessed += "-"

    print("\n\n\nWELCOME TO HANGMAN!")    
    print("The category is: ", lists[random_list])
    print("The secret word: ", guessed, "is", len(guessed), "letters")

    while Cont:
        your_guess = get_input("\nEnter your guess: ")
        if your_guess in word.lower():           
            for x in range(len(word)):
                if word[x].lower() == your_guess.lower():
                    guessed = guessed[:x] + word[x] + guessed[x+1:]
            guessed_letters.append(your_guess)
            print("\nThe secret word: ", guessed)
            if guessed.lower() == word.lower():
                print("\n\nCongratulations, you guessed the word!")
                play_again = input("\nWould you like to play again?(y/n) ")
                if play_again == "y" or play_again == "yes":
                    core_game()
                else:
                    Cont = False

        else:
            hang -= 1
            guessed_letters.append(your_guess)
            print("\nGuessed letters: ", guessed_letters)
            if hang == 5:
                hangman[2][3] = "O"
                print_hangman()
                print(guessed)
            elif hang == 4:
                hangman[3][3] = "|"
                print_hangman()
                print(guessed)
            elif hang == 3:
                hangman[3][2] = "-"
                print_hangman()
                print(guessed)
            elif hang == 2:
                hangman[3][4] = "-"
                print_hangman()
                print(guessed)
            elif hang == 1:
                hangman[4][2] = "/"
                print_hangman()
                print(guessed)
            elif hang == 0:
                hangman[4][4] = "\\"
                print_hangman()
                print("Game Over!")
                print("The word was: ", word)
                play_again = input("Would you like to play again?(y/n) ")
                if play_again == "y" or play_again == "yes":
                    core_game()
                else:
                    Cont = False


core_game()

主要函数是core_game(),它在程序运行时调用。在

1条回答
网友
1楼 ·

好的-是的,我喜欢这个游戏-在浏览器中小饰品.io在

然后我浏览了整个游戏here。使用它来调试您的代码,一步一步,您可以查看所有的变量,列表等,以及输入和查看输出。在

重复游戏的原因是:

1)在函数核心游戏中,您有以下代码:

if play_again == "y" or play_again == "yes":
    core_game()

这意味着,当你在第一场比赛后选择“是”时,你正在打开另一个函数的“实例”——如果你跟踪代码,你可以看到这一点。您也可以在下面的屏幕截图(f19)中看到这一点。在

2)在您玩第二个游戏(记住第一个实例仍然打开)并选择“n”之后,这会导致“Cont”变量变为False,但仅在第二个实例中。然后关闭“while”,代码返回到第一个Cont仍然为True的实例。您还可以在屏幕截图中看到这两个实例,以及每个值“Cont”的状态。封闭的框架“变灰”。在

3)程序在while循环开始时在核心游戏的第一个实例中恢复,并提示用户输入猜测。在

在您跟踪代码之前很难看到这一点,但是我在这里提供了一个快照,您可以(我希望)看到我的意思。在

Snapshot of game state after entering "no" for second game

所以问题是你从函数本身调用函数。在

考虑更像这样的事情:

^{pr2}$

PS公司 还有其他非常好的参考源,比如this。在

永远不要放弃-你会发现step-thru非常方便,你可以在以后的某个阶段尝试莱斯大学的MOOC课程。在

相关问题