-1

Here's the basic idea I'm having trouble with: I'm trying to make a simple game where you are in one room and you have 2 rooms branching from the first that need to be "completed" before continuing. I want the 2'nd and 3'rd room to change my original True statements to False statements that all need to be switched before proceeding in the game.

from sys import exit

def room_1():
    print "You're in room one, there are two doors to room 2 and 3."
    print "Where do you want to go?"

    done_2=True
    done_3=True

    while True:
        move=raw_input("'room 2' or 'room 3'? >")

        if move == 'room 2':
            room_2()
        elif move == 'room 3':
            room_3()
        else:
            print "not a valid answer"
    print "You Win!"
    exit(0)

def room_2():
    print "You finished room 2!"
    done_1=False
    raw_input('Press button')
    room_1()

def room_3():
    print "You finished room 3!"
    raw_input('press button')
    done_3=False
    room_1()

room_1()

How do I change the done_ statements from within rooms 2 and 3? ~

javex
  • 7,198
  • 7
  • 41
  • 60
  • 1
    Avoid `while True`. http://stackoverflow.com/a/4557326/59087 – Dave Jarvis Mar 13 '13 at 16:29
  • You could return a value from `room_2()` or `room_3()` and use this return value as the value of your `done_2` or `done_3` variables. But it looks like you should review software architecture and what kinds of things are possible with functions vs. what's possible with objects. – bdesham Mar 13 '13 at 16:30
  • If it was me I would make a class to store your done_ variables, then pass an instance of that between all room functions. That way you can edit them from anywhere because they all just have a reference to the instance. – max k. Mar 13 '13 at 16:31
  • Why don't you just `return True` from `room_2()` and `room_3()` and assign it to the `done_x` variable in the `room_1()` function. The way you do it now, you build up a stack of function calls where you probably don't want that. – javex Mar 13 '13 at 16:34

2 Answers2

4

In Python, you have to declare global variables before you can assign to them; otherwise, any assignment will shadow the global variable.

def room_2():
    global done_1  # <- right here
    print "You finished room 2!"
    done_1=False
    raw_input('Press button')
    room_1()

def room_3():
    global done_3 # <- right here
    print "You finished room 3!"
    raw_input('press button')
    done_3=False
    room_1()

However!

This is generally bad style, especially for such a simple case as this. It makes it more difficult to reason about how your functions work, what they change, and in what order.

It'd be much easier, more readable, and simpler to just return True or False from your functions as needed.

If you try to think of your functions as "black boxes" which have inputs, and guarantee certain outputs, it will generally help in avoiding many confusing bugs that can arise.

voithos
  • 68,482
  • 12
  • 101
  • 116
0

You need to declare done_1 and done_2 as global variables, outside of function room_1()

jcage
  • 651
  • 6
  • 14