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

慕桂英3389331
TA貢獻2036條經驗 獲得超8個贊
我找到方法了,分享出來
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貢獻1798條經驗 獲得超7個贊
呃,這樣?
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)))
添加回答
舉報
0/150
提交
取消