473,387 Members | 1,435 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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:

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
1 2391
bvdet
2,851 Expert Mod 2GB
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

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

Similar topics

2
by: Clive Foley | last post by:
Hey everyone. i have written a simple server that accepts user connections. these connections are made by a simple game which i also made. I would like this game to be played across a network...
0
by: dnphamus13 | last post by:
I'm new to this and drowning right now. I would like to put my database online for viewing. I managed to do the filtering but i need to do PAGING as the XML doc get bigger. From what i understand...
0
by: Valleriani | last post by:
Right Now I've been working on a text based game for fun. It was working fine but they I relized that you need to shut/close the ports. Before I was just opening ports and not shutting them, no...
10
by: connyledin | last post by:
Im trying to create a version of the game Wumpus. Mine is called Belzebub. But im STUCK! And its due tuesday 2 maj. Im panicing! Can some one help me?? here is the file:...
4
by: robinsand | last post by:
My apologies to those of you who are more advanced Visual C++ .NET programmers, but I am working on a project for an MBA course that is condensed into an eight-week schedule, and I need help...
0
by: raypjr | last post by:
Hi everyone. I need a little help with some parts of a word guessing game I'm working on. I have some parts done but unsure about others and could use a little advice. Any help is very much...
4
by: kimiraikkonen | last post by:
Hello dear experts, New to programming and while surfing the net to learn some examples, this C++ (maybe C, but source file name's extension is "cpp") game sample gave a lot of errors with the...
6
WhiteRider
by: WhiteRider | last post by:
def load(): # function to load game data if os.path.exists('./data.dat') == True: load = open('data.dat', 'r') save = load.readline() if save == "": welcome() elif save == "1\n"...
1
by: gresey4 | last post by:
i have a question how do i make in game accounts in my text based game EX(In a game like Hobo wars or Urbandead OR deadawaken they have small accounts for the people who registered and play their...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.