热线电话:13121318867

登录
2019-02-15 阅读量: 1064
python 如何try 多次?

python 如何try 多次?

try:
do someting
except:
os._exit(0)

比如执行do someting 时有错误,我想再试两次,如果两次执行还是有错误,那就os._exit(0) 退出

答:举个例子

import os
attempts = 0
success = False
while attempts < 3 and not success:

try:
do something
success = True
except:
attempts += 1
if attempts==3:
os._exit(0)

0.0000
4
关注作者
收藏
评论(0)

发表评论

暂无数据
推荐帖子