我正在努力研究Zed Shaw的学习python,我正在练习36.他希望我们制作一个类似于ex 35的游戏。这是我的代码的一部分。
def ninja_dojo():
print """
You enter a big room.
A ninja sits meditating between you and a door.
How do you get past the ninja?
"""
pass_ninja = False
while True:
move = raw_input('> ')
if move == 'walk quietly':
dead("As you pass the ninja, he springs up and guts you.")
elif move == 'greet ninja':
print "The ninja smiles and motions for you to pass."
arcade()
elif move == 'fight ninja':
dead("As you step forward to make your move, the ninja throws a Shuriken that slits your throat. You slowly bleed out.")
else:
dead("The ninja notices you and kills you, thinking you're an intruder. ooops.")
exit(0)
当我运行它并传递满足else块的东西时,我得到了这个错误。
Traceback (most recent call last): File "/Users/Anusha/Desktop/Anusha/Freshmore/Python/ex1.py", line 834, in <module>
start() File "/Users/Anusha/Desktop/Anusha/Freshmore/Python/ex1.py", line 829, in start
ninja_dojo() File "/Users/Anusha/Desktop/Anusha/Freshmore/Python/ex1.py", line 807, in ninja_dojo
exit(0) SystemExit: 0
有人可以向我解释一下吗?为什么不退出(0)在这里工作?这是Zed的代码,它运行得很好。
def gold_room():
print "This room is full of gold. How much do you take?"
next = raw_input("> ")
if "0" in next or "1" in next:
how_much = int(next)
else:
dead("Man, learn to type a number.")
if how_much < 50:
print "Nice, you're not greedy, you win!"
exit(0)
else:
dead("Your greedy bastard!")
我已经使用“来自sys导入退出程序启动了我的模块:就像他一样。 对于我的while循环,如果我使用break代替exit(0),它可以完美地工作。我的问题是为什么不退出(0)工作? 非常感谢提前!!
答案 0 :(得分:1)
sys.exit()
通过提升SystemExit
例外来工作。如果有任何东西捕获它或祖先,那么异常处理程序将运行。
答案 1 :(得分:0)
sys.exit(code)
引发SystemExit
例外。如果您想退出,可以使用break
或return