[Tutor] (no subject)
Danny Yoo
dyoo@hkn.eecs.berkeley.edu
Sun, 24 Feb 2002 21:13:20 -0800 (PST)
On Sun, 24 Feb 2002, Britt Green wrote:
> Can anyone tell me why I get this error on the following code, and what
> a good solution for it would be?
>
> >>>
> --> e
> You are in the foyer
> --> w
> Traceback (most recent call last):
> File "C:/WINNT/Profiles/bgreen/Desktop/kode/fourth.py", line 37, in ?
> if playerLoc.exits.has_key(command[0]):
> AttributeError: 'str' object has no attribute 'exits'
>
I'm guessing that 'playerLoc' is a Room:
> class Room:
> def __init__(self, name, exits):
> self.name = name
> self.exits = exits
> self.items = []
>
> def __str__(self):
> return self.name
>
> porch = Room('porch', {'e':'foyer'})
> foyer = Room('foyer', {'w':'porch', 'e':'dining room'})
> dining = Room('dining room', {'w':'foyer'})
When we try to look at the exits of a romm, like the porch, we'll find
that we get:
###
>>> porch.exits
{'e': 'foyer'}
>>> foyer
<__main__.Room instance at 0x814d72c>
###
So there's a difference between 'foyer' and the foyer room --- we actually
need to the the room instance whose name is 'foyer', and we need to make
that distinction.
Here's one way to approach the problem: We can make a dictionary of all
the rooms, so that once we have the name of an exit, we can get that exit
room.
###
all_rooms = {}
for name, room in [('porch', porch),
('foyer', foyer),
('dining', dining)]:
all_rooms[name] = room
###
Hope this helps!