如何在满足特定条件时使用 elif(或其他东西)突破此功能

How to use an elif (or something else) to break out of this function upon a certain condition being met

displayed_word = [" ", " ", " ", " "]
hangman = [":(", "-I", "-<", "you died!"]
guess_count = 0
secret_word = ["t", "e", "s", "t"]
miss_count = [-1]

def congratulations():
    print("congratulations, you solved it")

def hangman_start():
    guess = input("guess your letter:")
    secret_word = ["t", "e", "s", "t"]
    if guess == secret_word[0]:
        displayed_word[0] = guess
    if guess == secret_word[1]:
        displayed_word[1] = guess
    if guess == secret_word[2]:
        displayed_word[2] = guess
    if guess == secret_word[3]:
        displayed_word[3] = guess
    elif displayed_word == secret_word:
        print("congratulations")
    print(displayed_word)
    if guess not in secret_word:
        miss_count[0] += 1
        print(hangman[miss_count[0]])
    hangman_start()

hangman_start()

我的 'hangman' 游戏现在可以按照我想要的方式运行,但我无法弄清楚的一件事是如何让它在 miss_count 命中时退出“你死了!”或者 displayed_word 与 secret_word 相同。那时我可以让它打印语句,但是当我尝试在这些行下添加“break”时,我得到“break outside of loop”。是否有不同的命令来执行此操作?

break用于退出循环,如forwhile。这里你的“循环”实际上是递归,所以你想使用 return 使函数 return 成为一个值并停止,在我们的例子中我们只是 return 什么都没有,所以 None。这应该有效:

displayed_word = [" ", " ", " ", " "]
hangman = [":(", "-I", "-<", "you died!"]
guess_count = 0
secret_word = ["t", "e", "s", "t"]
miss_count = [-1]

def congratulations():
    print("congratulations, you solved it")

def hangman_start():
    guess = input("guess your letter:")
    secret_word = ["t", "e", "s", "t"]
    if guess == secret_word[0]:
        displayed_word[0] = guess
    if guess == secret_word[1]:
        displayed_word[1] = guess
    if guess == secret_word[2]:
        displayed_word[2] = guess
    if guess == secret_word[3]:
        displayed_word[3] = guess
    elif displayed_word == secret_word:
        print("congratulations")

        return # exit recursion

    print(displayed_word)
    if guess not in secret_word:
        miss_count[0] += 1
        print(hangman[miss_count[0]])

        if miss_count[0] == len(hangman) -1:
            return # exit recursion

    hangman_start()

hangman_start()

您要查找的关键字是return,这样使用

displayed_word = [" ", " ", " ", " "]
hangman = [":(", "-I", "-<", "you died!"]
guess_count = 0
secret_word = ["t", "e", "s", "t"]
miss_count = [-1]

def congratulations():
    print("congratulations, you solved it")

def hangman_start():
    guess = input("guess your letter:")
    secret_word = ["t", "e", "s", "t"]
    if guess == secret_word[0]:
        displayed_word[0] = guess
    if guess == secret_word[1]:
        displayed_word[1] = guess
    if guess == secret_word[2]:
        displayed_word[2] = guess
    if guess == secret_word[3]:
        displayed_word[3] = guess

    elif displayed_word == secret_word:
        print("congratulations")
        return

    print(displayed_word)
    if guess not in secret_word:
        miss_count[0] += 1

        if (your condition for "you died!" and ending the game):
            return

        print(hangman[miss_count[0]])
    hangman_start()

hangman_start()