0

I am very new to python. I am trying to learn by making a text based game. In the gameplay I am wanting a variable, door_open, to begin false but once unlocked door_open = True. The next time the function is called a different message would be displayed.

door_open =False
def house():    
    print "The door is locked"
    plan = raw_input(">")
    if plan.lower() == 'use key':
            inventory.remove("House Key")
        door_open = True
        print "You are inside"
        start() ## this just returns to the starting point outside
    if door_open == True:
        print "You walk inside"
        exit(0) ## using this until I develop it more 
    else:
        print "Door is locked. Do you want to use your key (Y/N)?"
Bi Rico
  • 25,283
  • 3
  • 52
  • 75

3 Answers3

3

When Python sees an assignment to a variable in a function, when the variable is not marked as global (or nonlocal), it treats the variable as a new local variable.

Thus Python is treating the original code similarly to

door_open = False
def house():    
    if plan.lower() == 'use key':
        door_open_LOCAL = True   # only assigned here
    if door_open_LOCAL == True:  # whoops, might not be assigned yet!
        pass

(That is, the local variable shadows the global variable, I've emphasized the point above by giving the local variable a different name entirely.)


See also:

Community
  • 1
  • 1
user2864740
  • 60,010
  • 15
  • 145
  • 220
2

If you want to write to a variable in the global scope you need to use the global keyword:

door_open = False
def house():    
    global door_open
    print "The door is locked"
    # ...

Also, please read and follow PEP8, the Python style guide. For example. you shouldn't use if foo == True: but if foo:

ThiefMaster
  • 310,957
  • 84
  • 592
  • 636
0

You are trying to track status of your game and got a bit lost in where is the status noted:

This is typical situation for using a class:

game.py:

import sys
class Game():
    def __init__(self):
        self.door_open = False
        self.inventory = ["House Key", "Snow Gun", "Swiss army knife"]
    def unlock_house(self):
        assert "House Key" in self.inventory, "`House Key` must be present in inventory"
        self.inventory.remove("House Key")
        print "Click, Click"
        self.door_open = True
    def fireup(self):
        print "Bufff"
        self.inventory = []

def main():
    game = Game()
    while True:
        # report current status
        if game.door_open:
            print "You are in the house!"
            print "You are the King"
            sys.exit(0)
        else:
            print "The door is locked!"
        # ask for further plans
        print "What is your further plan?"

        plan = raw_input(">")
        # change the situation
        if plan.lower() == "use key":
            game.unlock_house()
        elif plan.lower() == "set up file":
            game.fireup()

if __name__ == "__main__":
    main()

You can see the split between the class Game, which is able to track the status and even do some changes of it, and the main piece of code, where you try to use the game somehow.

The class Game is sort of idea, how your notes about game status could look like.

Then in game = Game() you instantiate the class, get an object, and start doing real changes to the status.

Having status information stored in well designed and quite well isolated object is very convenient, you know, where to find what you need.

Jan Vlcinsky
  • 42,725
  • 12
  • 101
  • 98