-2

I'm working on a text based adventure game in python. Nothing super fancy. I want to have a lever in 2 different rooms unlock a gate in a third room. Both levers need to be pulled in order for the gate to be unlocked.

here are the two rooms with the levers.

def SnakeRoom():

    choice = raw_input("> ")

    elif "snake" in choice:
        FirstRoom.SnakeLever = True
        print "As you pull the lever... You hear something click down the hall behind you."
        SnakeRoom()
    elif "back" in choice:
        FirstRoom()
    else:
        dead("Arrows shoot out from the walls. You don't make it.")

def WolfRoom():

    choice = raw_input("> ")

    elif "wolf" in choice:
        FirstRoom.WolfLever = True
        print "As you pull the lever... You hear something click down the hall behind you."
        WolfRoom()
    elif "back" in choice:
        FirstRoom()
    else:
        dead("Arrows shoot out from the walls. You don't make it.")

Here is the room with the gate.

def FirstRoom():
    Lever = WolfLever and SnakeLever
    choice = raw_input("> ")

    if "straight" in choice and Lever != True:
        print "You see a large gate in front of you. The gate is locked, there doesn't seem to be any way to open it."
        FirstRoom()
    elif "straight" in choice and Lever == True:
        SecondRoom()
    elif "left" in choice:
        WolfRoom()
    elif "right" in choice:
        SnakeRoom()
    elif "lever" in choice:
        print "WolfLever: %s" % WolfLever
        print "SnakeLever: %s" % SnakeLever
        print "Lever: %s" % Lever
        FirstRoom()

I shortened the code so you don't have to read through all the unnecessary stuff.

My biggest problem is I'm not super familiar with the Python language yet, so I'm not sure how to word everything to find the answers I'm looking for.

edit: Instead of FirstRoom.WolfLever I also tried just using WolfLever, in the body of my code, above Start() I have:

WolfLever
SnakeLever
Lever = WolfLever and SnakeLever

But my functions weren't updating these values. So I tried the FirstRoom. approach.

Ranger Skip
  • 105
  • 8
  • 1
    Why are you using only functions and no classes? I say this in the nicest way but you should probably spend more time learning python before you try and bite off a project like listed above. – Obj3ctiv3_C_88 Aug 05 '15 at 20:56
  • No offense taken. I'm following Learn Python The Hard Way, and this is how they have the book laid out. Haven't learned about classes yet (Although I know about them from my C++ experience) – Ranger Skip Aug 05 '15 at 20:58
  • I'm just going to say that this seems like a entirely odd thing to do. You should probably use a class (or, uh, an [Enum](https://docs.python.org/3/library/enum.html)) to hold this data. – NightShadeQueen Aug 05 '15 at 20:59
  • btw, `FirstRoom.WolfLever` is an attribute on the function `FirstRoom`. It's not a variable in `FirstRoom`'s closure. – NightShadeQueen Aug 05 '15 at 21:00
  • 2
    This question is probably too broad to answer because you have several fundamental misconceptions about how a program this large should be laid out. That's OK because you're just a beginner, but in the space of a single StackOverflow question and answer it's going to be difficult to disentangle it. Basically, though, you're having trouble because you're trying to make functions into classes, and that's just not what they are. – Two-Bit Alchemist Aug 05 '15 at 21:03
  • 1
    As @Obj3ctiv_C_88 said it would be advantageous to learn classes, but in this case have you tried the `globals` statement? – Anthony Aug 05 '15 at 21:03

1 Answers1

1

Credit to @Anthony and the following link: Using global variables in a function other than the one that created them

Globals definitely were the answer (With the exception of using classes). Here's what my WolfRoom() and SnakeRoom() functions look like now:

def WolfRoom():
    global WolfLever

    choice = raw_input("> ")

    elif "wolf" in choice:
        WolfLever = True
        print "As you pull the lever... You hear something click down the hall behind you."
        WolfRoom()

For FirstRoom() I added

global Lever

to the beginning of the function and right before Start() I have

WolfLever = False
SnakeLever = False

this way I have no errors or warnings (Was getting syntax warnings for assigning a value to my levers before declaring them as global) and everything works perfectly.

Community
  • 1
  • 1
Ranger Skip
  • 105
  • 8