[Tutor] (no subject)

Kirby Urner urnerk@qwest.net
Sun, 24 Feb 2002 21:09:23 -0800


>
>while 1:
>     command = string.split(raw_input('--> '))
>     if command[0] in commands:
>         if playerLoc.exits.has_key(command[0]):
>             playerLoc = playerLoc.exits.get(command[0])
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

This line is resetting playerLoc to the name of the
room, not to an actual room object.  So next time
through the loop, playerLoc has no attribute 'exits'.

You could store all your rooms in a dictionary, by
name, thereby making it easier to lookup the object you
need when the user hits a control key, e.g.:

rooms  = {"porch" : Room('porch', {'e':'foyer'}),
           "foyer" : Room('foyer', {'w':'porch', 'e':'dining room'}),
           "dining room" : Room('dining room', {'w':'foyer'})
          }

commands = ['n','s','e','w']
playerLoc = rooms["porch"]

print "You are in the", playerLoc
while 1:
     command = string.split(raw_input('--> '))
     if command[0] in commands:
         if playerLoc.exits.has_key(command[0]):
             playerLoc = rooms[playerLoc.exits.get(command[0])]
             print "You are in the", playerLoc


==========
Testing:

 >>> reload(test2)
You are in the porch
--> w
--> e
You are in the foyer
--> e
You are in the dining room
--> e
--> w
You are in the foyer

and so on.

Kirby