我正在尝试从艰难的方式学习python中的一个练习,并且我坚持做某事。我创建了一个函数,如果其中一个语句已经完成,我想把它放在另一个函数中。这是我如何尝试这样做的概述:
def room_1():
print "Room 1"
button_push = False
while True:
next = raw_input("> ")
if next == "1":
print "You hear a click but nothing seems to happen."
button_push = True
elif next == "2":
print "You enter room 2"
room_2()
def room_2():
print "Room 2"
while True:
next =raw_input("> ")
if next == "1" and button_push:
print "You can enter Room 3"
room_3()
如果满足了button_push,那么我希望在room_2中看到它。有人可以帮帮我吗?
答案 0 :(得分:1)
您可以将button_push
作为参数传递到下一个房间:
def room_1():
print "Room 1"
button_push = False
while True:
next = raw_input("> ")
if next == "1":
print "You hear a click but nothing seems to happen."
button_push = True
elif next == "2":
print "You enter room 2"
room_2(button_push) # pass button_push as argument
def room_2(button_push): # Accept button_push as argument
print "Room 2"
while True:
next =raw_input("> ")
if next == "1" and button_push: # button_push is now visible from this scope
print "You can enter Room 3"
room_3()