[Tutor] Picking up Item()s

Lloyd Hugh Allen lha2@columbia.edu
Sun, 17 Feb 2002 20:01:08 -0500


Britt Green wrote:
> 
> I have the following code for a very very simple text adventure. Right
> now a player can move around a few rooms and there are a couple of
> objects in the room that one should be able to pick up.
> 
> Unfortunately I'm stuck on this point: When a player enters a room it
> doesn't say "This room contains a key." Instead it says "This room
> contains  [<__main__.Item instance at 0x00AC5320>]". How can I make it
> so that it displays the name of the Item. Furthermore, I'd want the
> player to be able to type "get key" to pick up the key, not
> "__main__.Item instance at 0x00AC5320". How could I do this?

it works a /little/ better if you append a .name in the line

###
for n in items:
        things.append(Item(n)).name
###

and then in order to avoid an error when picking up items, you have to
drop the .name in

###
        else:
            if command[1] in rooms[playLoc].items:
                print "You picked up the", command[1]
###

You also may wish to prettify the display by using the

", ".join(ListOfThings)

convention so that the user doesn't see the list of exits etc. with
funny brackets. Would have to get more complicated in order to throw an
"and" in there for the last item, but at least it doesn't break on a
single-element list.

Pretty neat stuff, though, what you have so far.