当我为朋友们在python中进行文本冒险时,由于python从上到下读取代码,因此我不得不向上编码冒险。例如:
def stage2():
def stage1():
def start():
然后依旧等等。有什么方法可以让python以不同的方式读取代码,以便我可以自上而下编写文本冒险?
答案 0 :(得分:4)
您可以按照自己喜欢的顺序编写函数,只要它们定义,然后才能 。
例如,
def start():
print("Welcome to the adventure")
stage1()
def stage1():
print("You made it this far!")
stage2()
# if you called start() here,
# you would have an error when stage1() tries to call stage2(),
# because the interpreter doesn't know what a stage2 is yet.
def stage2():
print("Oops - you died.")
start()
工作正常。
另一方面:
def test():
if True:
print("Yup")
else:
slartibartfast()
test()
运行正常,因为解释器永远无法询问什么是slartibartfast; - )