简体   繁体   English

如何在其他函数中的函数中调用变量?

[英]How can I call a variable from a function within a different function?

I am currently trying to create a text based game. 我目前正在尝试创建一个基于文本的游戏。 I am trying to make it so when you pick up items you have variable which reads 'True' so you can use the items in a different room. 我正在努力做到这一点,因此当您拿起物品时,您的变量将显示为“ True”,因此您可以在其他房间中使用这些物品。 The problem I have currently is in one room you kill a monster so I create a variable in which the monster_is_dead = True, But once I move onto another function, the script forgets that the monster_is_dead.it is difficult to explain a lot of script to paste in but I will try and pick out the part of the script I am having trouble with. 我目前遇到的问题是在一个房间里杀死一个怪物,所以我创建了一个其中monster_is_dead = True的变量,但是一旦我移至另一个函数,脚本便忘记了monster_is_dead。很难解释很多脚本来粘贴,但我会尝试选择遇到问题的脚本部分。

def room_2():
riddle_monster_alive = True
while riddle_monster_alive:
    print 'As you step into the room, you feel someone watching you.. You close the door behind you and hear the door click.\n you are locked in!'
    print "You look up into the center of the room to be greeted by a strange creature,a Kappa"
    print 'the creature calls out to you "You are lost here, You could die here. I could kill you now for my Master, but that is no fun."'
    print '"How about we play a game? if you win, I will go, if you lose, I will eat you right now"'
    import random
    a = 'rock'
    b = 'paper'
    c = 'scissors'
    rockpaperscissorlist = [a,b,c]
    print 'The Kappa are known for their trickery and games, you best play his game'
    print '"Rock,paper and scissors we play till one of us loses"'
    game_choice = raw_input('Pick A:Rock B:Paper C:Scissors [a/b/c]\n>').lower()
    kappa_choice = random.choice(rockpaperscissorlist)
        elif game_choice == 'a' and kappa_choice == c:
        print 'Kappa picks scissors'
        print 'You Win!'
        riddle_monster_alive = False
        break
riddle_monster_alive == False
room_2a()

So I have a new function for room 2, in which the monster is dead. 因此,我为房间2提供了一个新功能,其中怪物已死。 However the script gets complicated as I let the player move back rooms as they will end up in function room_2() again and have to play against the monster again. 但是,当我让玩家将房间移回房间时,脚本变得很复杂,因为它们将再次出现在功能room_2()中,并且必须再次与怪物对战。

def room_2a():
print 'The air is still as the Kappa has now vanished.'
print 'You see a door the right and a door to the left'
print 'What will you do?\n A:Take the door to the right\n B:Take the door to the left?\n C:Go back the way you came\n D:Search the room'
room2choice = raw_input('> ').lower()
if room2choice == 'a':
    room_10()
elif room2choice == 'b':
    room_3()
elif room2choice == 'c':
    room_1a()
elif room2choice == 'd':
    print 'You searched but found nothing but a puddle of water from where the Kappa once stood'
    room_2a()

I feel like I am making this more complicated than necessary. 我觉得这让事情变得不必要了。

You cannot call a variable local to a function (defined within only the context of that function) from outside the function. 您不能从函数外部调用函数局部变量(仅在该函数的上下文中定义)。 This violates scope rules. 这违反了范围规则。 A variable declared in a function is different from the one defined outside. 函数中声明的变量与外部定义的变量不同

For instance, 例如,

x = 10 # global variable, defined outside function
def f():
    x = 5 # creates a new local variable, defined inside function
    return # local variable is destroyed
f()
print(x)
>>>> 10 # global variable is returned and unaffected

You need to declare that you want the variable to be global (ie accessible everywhere) using the global keyword. 您需要使用global关键字声明要使变量为全局变量(即可以在任何地方访问)。

eg 例如

x = 10
def f():
    global x # lets Python know you want to assign a value to a global variable
    x = 5 # now you're changing global variable x, outside the function
    return
f()
print(x)
>>>> 5 # global variable is changed

In your case, you want to do 就您而言,您想做

def room_2():
    global riddle_monster_alive # declares this variable global, so room_2() can change the global value
    riddle_monster_alive = False # makes this variable False everywhere
    ... # continue as before.

That Being Said... 话虽如此...

Using global variables is a known anti-pattern. 使用全局变量是一种已知的反模式。 You should avoid making use of this construct as much as possible. 您应该避免尽可能多地使用此构造。

As Matt S. points out in the comments, using objects and classes is a much better solution. 正如Matt S.在评论中指出的那样,使用对象和类是更好的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM