I am learning Python classes by writing an adventure game. I have extracted just the relevant code. The player can look, go, drop, take, inventory but not examine.
Python produces the error message in the Player class, examine function, in the first print statement. How do I fix this code, please? class Player(object): """The player in the game.""" def __init__(self, name, currentRoom=None): self.name = name self.currentRoom = currentRoom self.items = [] # items taken by Player def look(self): """Prints the room description, exits and items in the current room.""" print self.currentRoom def go(self, direction): """Go to another room through an exit.""" def drop(self, item): """Drop an item you are carrying in the current room.""" def take(self, item): """Take (pick up) an item from the current room.""" if item == self.currentRoom.item: self.items.append(item) print self.currentRoom.item + " added to player's inventory..\n" # remove item from currentRoom self.currentRoom.item = '' else: print "There is no " + item + " here to take!\n" def inventory(self): """Prints list of items the player is carrying.""" def examine(self, item): """Prints description of item.""" if item in self.items: # Error: 'str' object has no attribute 'description' print "Item Description: " + item.description + ".\n" else: print "You are not holding the " + item + " to examine!\n" class Item(object): """A useful item in the game.""" def __init__(self, name, description, location): self.name = name self.description = description self.location = location def __str__(self): """Description of item.""" return self.description class Game(object): """The adventure game.""" def __init__(self, name, player): self.name = name self.player = player print "Welcome to " + self..name + "\n" print player.currentRoom def play(self, character): """Command loop for game.""" while True: # loop forever commands = raw_input("-> ").lower() if not commands: print "Huh?" else: try: cmdlist = commands.split() # separate commands action = cmdlist[0] if action == "quit": print "See you later!" break elif action == "look": elif action == "go": elif action == "take": elif action == "drop": elif action == "examine": if len(cmdlist) != 2: print "An examine command must be followed by an item name." continue else: character.examine(cmdlist[1]) elif action == "inventory": except AttributeError, msg: print "Error - undefined method: " + str(msg) def main(): # Room descriptions room5 = Room(name="hallway", description="You are in a dark hallway.", item="diary") # Item descriptions diary = Item(name="diary", description="grey-covered Croxley diary", location=room5) _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor