首页 > 解决方案 > python - 循环中断不会第二次工作

问题描述

def play_again():
    again = input("would you like to play another game? ").lower()
    while play_again:
        if again == "yes":
            play_again()
        elif again == "no":
            print("thank you for playing with us")
            break
        else:
            again = input("only yes, or no answers are allowed: ").lower()
            continue

play_again()

如果我运行代码并输入 no,代码将退出。

如果我运行代码并输入yes,代码继续,但如果第二个输入是no,代码将不会退出:(

有人能帮我吗?

标签: pythonpython-3.x

解决方案


感谢那些回答的人,我删除了while循环并选择了一种现在可以使用的不同方式:

    def play_again():
    again = input("would you like to play another game? ").lower()
    if again == "yes":
        play_again()
    elif again == "no":
        print("thank you for playing with us")
    else:
        print("only yes, or no answers are allowed")
        play_again()


play_again()

推荐阅读