在Python中创建失败状态

时间:2015-10-11 01:09:55

标签: python python-2.7

elif schoice2 == 2:
    print "You run away from the bear about as fast as Usian Bolt, even though you have no clue who that is since it's the year 1112, but you decide not to dwell on who or what a 'Usian Bolt' is. The bear gives chase, then ultimitely crashes down on you, giving you a fast death."
    print "YOU ARE DEAD, TRY AGAIN!"

所以我想知道是否有一个命令可以完全停止正在运行的脚本。

1 个答案:

答案 0 :(得分:0)

要退出代码中的脚本,您必须执行此操作:

sys.exit()函数将退出程序,并将显示错误消息。这是按要求执行的功能。

import sys
#do some stuff
elif schoice2 == 2:
    print "You run away from the bear about as fast as Usian Bolt, even though you have no clue who that is since it's the year 1112, but you decide not to dwell on who or what a 'Usian Bolt' is. The bear gives chase, then ultimitely crashes down on you, giving you a fast death."
    print "YOU ARE DEAD, TRY AGAIN!"
    sys.exit()

编辑:

quit()函数不会给出错误消息,只会结束程序。

新代码如下所示:

import sys
#do some stuff
elif schoice2 == 2:
    print "You run away from the bear about as fast as Usian Bolt, even though you have no clue who that is since it's the year 1112, but you decide not to dwell on who or what a 'Usian Bolt' is. The bear gives chase, then ultimitely crashes down on you, giving you a fast death."
    print "YOU ARE DEAD, TRY AGAIN!"
    quit()