【发布时间】:2021-06-17 22:26:26
【问题描述】:
程序在我想要的地方结束,并带有正确的 SystemExit 消息,但是在 SystemExit 之前的行中出现了一个 Traceback,显示了它退出的行。有没有办法只显示 SystemExit 和没有回溯的消息?我在程序的开头有“import sys”。
本节的代码是:
if guess == 'q':
print('q')
sys.exit('You exited the game')
不需要的输出说:
Traceback (most recent call last):
File "filelocation\programname.py",
line 38 in <module>
sys.exit('You exited the game')
想要的输出后面一行说:
SystemExit: You exited the game
【问题讨论】: