A problem with my text based game

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • cousinoer5
    New Member
    • Mar 2009
    • 1

    A problem with my text based game

    I'm trying to implement a shop in my game, right now I can buy anything in the shop and it works perfectly, but I can't sell anything because I get an error I can't figure out.

    Here's the relevent code:

    Code:
        def shop(self, name):
            self.name = name
            if name == "sapphire":
                if "sapphire" in self.inventory:
                    print "I can buy that for 10 gold."
                    self.inventory.remove_item("sapphire")
                    self.gold += 10
                    print "You now have", self.gold, "gold."
                else:
                    print "You don't have a", name
            elif name == "emerald":
                if "emerald" in self.inventory:
                    print "I can buy that for 20 gold."
                    self.inventory.remove_item("emerald")
                    self.gold += 20
                    print "You now have", self.gold, "gold."
                else:
                    print "You don't have an", name
            elif name == "ruby":
                if "ruby" in self.inventory:
                    print "I can buy that for 30 gold."
                    self.inventory.remove_item("ruby")
                    self.gold += 30
                    print "You now have", self.gold, "gold."
                else:
                    print "You don't have a", name
            elif name == "diamond":
                if "diamond" in self.inventory:
                    print "I can buy that for 40 gold."
                    self.inverntory.remove_item("diamond")
                    self.gold += 40
                    print "You now have", self.gold, "gold."
                else:
                    print "You don't have a", name
            elif name == "onyx":
                if "onyx" in self.inventory:
                    print "That's a really rare gem! \nI'll give you 50 gold for it."
                    self.inventory.remove_item("onyx")
                    self.gold += 50
                    print "You now have", self.gold, "gold."
                else:
                    print "You don't have an", name
            else:
                print "I can't buy that."
    I'm trying to call this function in a text game base program I imported into this program:

    Code:
        def remove_item(self, item):
            del self[item.name]
    Now when I actually go to sell any one of those items, this error occurs:

    Traceback (most recent call last):
    File "J:\Alex and the Mysterious Temple.py", line 594, in ?
    main()
    File "J:\Alex and the Mysterious Temple.py", line 592, in main
    adv.play("Alex and the Mysterious Temple")
    File "E:\text_game.p y", line 160, in play
    self.player.mov e(verb)
    File "J:\Alex and the Mysterious Temple.py", line 263, in move
    self.shop(sell)
    File "J:\Alex and the Mysterious Temple.py", line 163, in shop
    self.inventory. remove_item("sa pphire")
    File "E:\text_game.p y", line 32, in remove_item
    del self[item.name]
    AttributeError: 'str' object has no attribute 'name'

    Help is appreciated.
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    The error message tells you what the problem is. You are in effect trying to do this:
    Code:
    del self["ruby".name]
    Maybe something like this would work:
    Code:
    del self.inventory[item]

    Comment

    Working...