from sys import exit
def gold_room():
print "This room is full of gold. How much do you take?"
choice = raw_input("> ")
if "0" in choice or "1" in choice:
how_much = int(choice)
else:
dead("Man, learn to type a number.")
if how_much < 50:
print "Nice, you're not greedy, you win!"
exit(0)
else:
dead("You greedy bastard!")
def dead(why):
print why
exit(0)
1)如何在dead
方法内部调用gold_room
方法,因为方法的定义dead
在调用语句下方?
2)如何在变量how_much
范围之外访问变量?它是在缩进块中声明和初始化的-
if "0" in choice or "1" in choice:
how_much = int(choice)
并且根据我的理解,它的范围应该在此处结束。那么在这种情况下如何进一步使用它if how_much < 50
?
Python没有内置“死”方法。解释器告诉您未定义的原因是因为您尚未定义。
Python没有阻止范围。从那时起,函数内部定义的变量将可见,直到函数结束。
更改第一个问题后进行编辑定义函数的顺序无关紧要,只要它们在调用时都已定义即可。大概gold_room
在两个函数都定义之后,在该模块的末尾有一些代码会调用。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句