try: do sometingexcept:
os._exit(0)比如執(zhí)行do someting 時(shí)有錯(cuò)誤,我想再試兩次,如果兩次執(zhí)行還是有錯(cuò)誤,那就os._exit(0) 退出
2 回答

慕桂英3389331
TA貢獻(xiàn)2036條經(jīng)驗(yàn) 獲得超8個(gè)贊
我找到方法了,分享出來
import os attempts = 0success = False while attempts < 3 and not success: try: do something success = True except: attempts += 1 if attempts==3: os._exit(0)

元芳怎么了
TA貢獻(xiàn)1798條經(jīng)驗(yàn) 獲得超7個(gè)贊
呃,這樣?
def retry_do(func, retry=None): try: func() except: if retry: retry() else: os._exit(0)def do_sth(): #do something retry_do(do_sth, retry_do(do_sth, retry_do(do_sth)))
添加回答
舉報(bào)
0/150
提交
取消