【发布时间】:2016-09-24 08:51:06
【问题描述】:
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) gold_room 方法内部如何调用dead 方法,因为dead 方法的定义在调用语句下面?
2) 变量how_much 如何在其范围之外访问?它在缩进块内声明和初始化 -
if "0" in choice or "1" in choice:
how_much = int(choice)
而且根据我的理解,它的作用域应该到这里就结束了。那么在这种情况下如何进一步使用它 - if how_much < 50 ?
【问题讨论】:
标签: python python-2.7 scope