Python- text based game not calling the correct room
Question: I am writing a text based game and I want to link each room to four other
rooms- north, south, east and west. I am starting with just north for now. The
user should be able to type 'walk north' and the north room should be called.

I have used three files- one where I will write the main story, one to call
the appropriate room within the story and one for navigation to avoid mutual
importing.

rooms.py:

    
    
    import actions
    
    class FirstRoom(object):
    
        room_name = 'FIRST ROOM'
        north = 'north_room'
    
        def __init__(self):
            pass
    
        def start(self):
            print self.room_name
            while True:
                next = raw_input('> ')
                actions.walk(next, self.north)
                actions.command(next)
    
    
    class North(object):
    
        room_name = "NORTH ROOM"
    
        def __init__(self):
            pass
    
        def start(self):
            print self.room_name
    

actions.py:

    
    
    import navigation
    
    def walk(next, go_north):
        """Tests for 'walk' command and calls the appropriate room"""
        if next == 'walk north':
            navigation.rooms(go_north)
        else:
            pass
    

navigation.py:

    
    
    import rooms
    first_room = rooms.FirstRoom()
    north_room = rooms.North()
    
    def rooms(room):
        rooms = {
            'first_room': first_room.start(),
            'north_room': north_room.start(),
            }
        rooms[room]
    

When I run first_room.start() it should print 'FIRST ROOM' which it does. Then
I type in 'walk north' and I expect it to print "NORTH ROOM", but instead it
prints "FIRST ROOM" again.

I can't figure out for the life of me why it doesn't work the way I expect it
to, it's as if it's calling first_room again instead of north_room. Can anyone
figure out what I'm doing wrong?


Answer: My guess is that the issue occurs because of how the dictionary `rooms` is
defined. When you do -

    
    
    rooms = {
        'first_room': first_room.start(),
        'north_room': north_room.start(),
        }
    rooms[room]
    

The functions get called when you define the dictionary itself, not when you
access the values from it (so both functions get called) , you want to store
function objects (without calling them) as the values and then call them as -
`rooms[room]()` . Example -

    
    
    def rooms(room):
        rooms = {
            'first_room': first_room.start,
            'north_room': north_room.start,
            }
        rooms[room]()
    

