HE2021 发表于 2022-12-5 10:42:51

Python程序终止

python程序运行完,结果已经出来了,但是程序不退出,提示Ctrl-c终止程序,可以加什么代码,让它自动退出吗?

wp231957 发表于 2022-12-5 10:48:09

源代码???

suchocolate 发表于 2022-12-5 11:02:11

本帖最后由 suchocolate 于 2022-12-5 11:04 编辑

常规主动提前退出程序,推荐用exit或抛出异常
for i in range(10):
    print(i)
    if i == 5:
      exit()
      # raise Exception('exit with 5')


或者断言
for i in range(10):
    print(i)
    assert not i == 5


不过最好先贴出你的代码,具体问题具体分析。
页: [1]
查看完整版本: Python程序终止