-1

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. 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.

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. 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.

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.

  • 1
    The living-or-death state of your monster should be a global variable. – Jongware Apr 27 '16 at 18:40
  • 1
    I think you'll want to consider using object-oriented programming, which will let you preserve state. – Matt S Apr 27 '16 at 18:41
  • At the start of my code the state of the monster is True but I am not sure how to change that to False after the function has been run, I guess that's my problem. – Kingbuttmunch Apr 27 '16 at 18:42
  • 1
    Possible duplicate of [Python: Passing variables between functions](http://stackoverflow.com/questions/16043797/python-passing-variables-between-functions) – Tadhg McDonald-Jensen Apr 27 '16 at 18:45

1 Answers1

2

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 (i.e. accessible everywhere) using the global keyword.

e.g.

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.

Akshat Mahajan
  • 9,543
  • 4
  • 35
  • 44
  • 1
    "so everyone can access it" might be a bit misleading since other functions can legally have local variables with the same name, it might be more accurate to say "so that this function can change the global value of this variable." – Tadhg McDonald-Jensen Apr 27 '16 at 18:50
  • 1
    no global statement is needed to "access" a global variable, the global statement allows the function to "assign a value to" global variables. – Tadhg McDonald-Jensen Apr 27 '16 at 18:51
  • @TadhgMcDonald-Jensen You're right, I got caught up in writing this. Making edits now... – Akshat Mahajan Apr 27 '16 at 18:52