2017-06-02 252 views
0

我正在开发一款游戏,并且卡住了钥匙应该解锁房间门的部分。现在,经过一番搜索之后,我发现我无法将一个函数中存在的变量调用到另一个函数中:正如我一直试图通过在厨房函数的条件下设置key_picked = True一样。然后,使用房间函数中的条件,并使用布尔表达式中的key_picked如何将变量从另一个函数调用到另一个函数中

那么,我该如何解决这个问题?

def kitchen(already_there=False): 
    if choice02_key == "pick key" or choice02_key == "0" or choice02_key == "key": 
     print("---------------------------------------------------------") 
     print "You picked the key. It probably unlocks some door." 
     key_picked = True 
     kitchen(already_there=True) 



def room01(already_there=False): 
    if key_pick == True: 
     print("---------------------------------------------------------") 
     print "You unlocked the room using the key." 
    else: 
     print "This room is locked." 
     entrance_hall(already_there=True) 
+1

这是一个很大的代码!如何把它煮成几行的例子。只是简单地展示问题。 – tdelaney

+0

听起来不错。我将编辑它 –

+0

将变量传递给函数?这个问题并不完全清楚。 –

回答

0

您可以在参数中传递变量。例如:

  1. 定义keyPickedroom01
  2. 请致电kitchen(already_there, keyPicked)room01
    1. 做出你想要的任务。
  3. 然后,您将在keyPicked中获得想要的值。

例如,想象一下,我有一个函数将数字加10。这会更好地回报价值,但它只是向你展示如何做到这一点。

def add_ten(number): 
    number = number + 10 

def main(): 
    number = 5 
    print('Number is:', number) 
    add_ten(number) 
    print('Number is:', number) 

输出:

Number is: 5 
Number is: 15 
相关问题