退出python程序的最佳方法? [英] Best way to quit a python programme?

查看:63
本文介绍了退出python程序的最佳方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我觉得自己是个白痴,但是这样做是终止python程序的最佳方法吗?还是有一种更好的方法可以在True循环发生时逐渐停止所有操作,而不是立即停止所有操作?再一次,我觉得自己是个白痴,但是我很好奇.

I feel like an idiot asking this but is doing quit() the best way to terminate a python programme? Or is there a better way that could gradually stop all while True loops ect instead of just instantly stopping it all? Once again, I feel like an idiot asking this but I'm just curious.

推荐答案

我不知道您为什么不想使用 quit(),但是您可以使用以下方法:

I don't know why you don't want to use quit() but you can use this:

import sys
sys.exit()

或者这个:

raise SystemExit(0)

要暂停一会儿循环,可以使用 break 语句.例如:

To halt a while loop you can use the break statement. For example:

while True:
    if True:
        do something  #pseudocode
    else:
        break

Python读取 else 语句后, break 语句将立即暂停 while 循环

The break statement will immediately halt the while loop as soon as the else statement is read by python

这篇关于退出python程序的最佳方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆