By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,251 Members | 2,727 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,251 IT Pros & Developers. It's quick & easy.

A problem with my text based game

P: 1
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:

Expand|Select|Wrap|Line Numbers
  1.     def shop(self, name):
  2.         self.name = name
  3.         if name == "sapphire":
  4.             if "sapphire" in self.inventory:
  5.                 print "I can buy that for 10 gold."
  6.                 self.inventory.remove_item("sapphire")
  7.                 self.gold += 10
  8.                 print "You now have", self.gold, "gold."
  9.             else:
  10.                 print "You don't have a", name
  11.         elif name == "emerald":
  12.             if "emerald" in self.inventory:
  13.                 print "I can buy that for 20 gold."
  14.                 self.inventory.remove_item("emerald")
  15.                 self.gold += 20
  16.                 print "You now have", self.gold, "gold."
  17.             else:
  18.                 print "You don't have an", name
  19.         elif name == "ruby":
  20.             if "ruby" in self.inventory:
  21.                 print "I can buy that for 30 gold."
  22.                 self.inventory.remove_item("ruby")
  23.                 self.gold += 30
  24.                 print "You now have", self.gold, "gold."
  25.             else:
  26.                 print "You don't have a", name
  27.         elif name == "diamond":
  28.             if "diamond" in self.inventory:
  29.                 print "I can buy that for 40 gold."
  30.                 self.inverntory.remove_item("diamond")
  31.                 self.gold += 40
  32.                 print "You now have", self.gold, "gold."
  33.             else:
  34.                 print "You don't have a", name
  35.         elif name == "onyx":
  36.             if "onyx" in self.inventory:
  37.                 print "That's a really rare gem! \nI'll give you 50 gold for it."
  38.                 self.inventory.remove_item("onyx")
  39.                 self.gold += 50
  40.                 print "You now have", self.gold, "gold."
  41.             else:
  42.                 print "You don't have an", name
  43.         else:
  44.             print "I can't buy that."
  45.  
  46.  
I'm trying to call this function in a text game base program I imported into this program:

Expand|Select|Wrap|Line Numbers
  1.     def remove_item(self, item):
  2.         del self[item.name]
  3.  
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.py", line 160, in play
self.player.move(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("sapphire")
File "E:\text_game.py", line 32, in remove_item
del self[item.name]
AttributeError: 'str' object has no attribute 'name'

Help is appreciated.
Mar 7 '09 #1
Share this Question
Share on Google+
1 Reply


bvdet
Expert Mod 2.5K+
P: 2,851
The error message tells you what the problem is. You are in effect trying to do this:
Expand|Select|Wrap|Line Numbers
  1. del self["ruby".name]
Maybe something like this would work:
Expand|Select|Wrap|Line Numbers
  1. del self.inventory[item]
Mar 8 '09 #2

Post your reply

Sign in to post your reply or Sign up for a free account.