首页 > 解决方案 > Break Program while in a while true - try and except

问题描述

如果用户输入“否”,我需要我的程序中断。此时程序不会中断,当输入'no'时,try and except 重新启动

while final_answer_check == True:

try:
    final_answer = str(input("Do you want a copy of the answers?"))
    if final_answer.lower() == "no":
        final_answer_check = False

我希望程序会中断,但它只是问“你想要答案的副本吗?” 再次

标签: python

解决方案


首先,您需要定义变量final_answer_check并将值设置为True. 如果你在一个块中构建你的代码try...except,你需要让它完整,而不仅仅是try.

final_answer_check = True
while final_answer_check == True:
    try:
        final_answer = str(input("Do you want a copy of the answers?"))
        if final_answer.lower() == "no":
            final_answer_check = False
        else:
            final_answer_check = True
    except:
        print ("your another code should be here")

推荐阅读