暂时中断程序-尝试并除外

时间:2019-03-26 05:40:51

标签: python

如果用户输入“否”,我需要中断程序。目前,该程序不会中断,并且输入“ no”时,try和except重新启动

class

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

2 个答案:

答案 0 :(得分:1)

从评论继续,这应该做:

const date = new Date();
const today = date.getUTCFullYear().toString()+ (date.getUTCMonth()+1)+date.getUTCDate().toString();


export const taskSchema = new Schema ({
dateCreated: {
     
        type: String,
        default: today
    }
});

编辑

但是,更好的方法可能是使用带有final_answer_check = True # a boolean flag while final_answer_check: # while the flag is set to true try: final_answer = str(input("Do you want a copy of the answers?")) if final_answer.lower() == "no": final_answer_check = False except: pass 的无限循环:

break

输出

while True:
    try:
        final_answer = input("Do you want a copy of the answers?")
        if final_answer.lower() == "no":
            break
    except:
        pass

答案 1 :(得分:1)

首先,您需要定义变量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")