Obviously, it doesn't work. So what I'm trying to do (this is all very rough at the moment, merely an experiment/test to see if I can get it to work) is have a method under the player class that, when called, takes the value of the requested item, sees if it exists and is valid (which works), and then if both are true then executes the use method on the target item. The problem is, I can't figure out how to pass the name of the desired item in the player's list inventory without it being a string.

Sorry if this is a really nooby mistake. All criticism welcome.

class player():
	def __init__(self):
		self.inv = []
		self.health = 10
	def hurt(self):
		print "Hurting player for 1 damage."
		self.health =- 1
	def use(self):
		usein = raw_input("Use what? >> ")
		if self.inv.count(usein) < 1:
			print "Item doesn't exist!"
		elif usein is int:
			print "Items aren't numbers!"
	def print_inv(self):
		slot1 = self.inv[0]
		print "1. ", slot1,"  ", self.inv.count(slot1)

class apple():
	def __init__(self):
		self.weight = 5
		self.value = 2
		self.hp_restore = 1
	def new_apple(self):
	def use(self):
		bob.health += self.hp_restore
5 Years
Discussion Span
Last Post by Zaffron

I'm not sure why you're having trouble passing strings, but it certainly ought to work. For example:

def printString(my_str):

I suspect where you're having trouble is in passing values to methods, but that too should be straightforward; you simply put the arguments after the self argument.

class Player():
    def __init__(self, inithealth = 10):
        self.inv = []
        self.health = inithealth
    def hurt(self, damage):
        print "Hurting player for {0} damage.".format(damage)
        self.health -= damage
    def use(self, item):
        if type(item) is not str:
            raise TypeError
        elif self.inv.count(item) < 1:
            print 'There is no {0} in your inventory.'.format(str(item))
    def print_inv(self):
        for slot, count in enumerate(inv):
            print count, + '. ' + str(slot)

if __name__ == '__main__':
    player = Player()
    print r"Player's health is now at", player.health

(BTW, it is a convention in Python to capitalize the first letter in the name of a class. While you don't have to follow this convention, it will probably save you trouble and confusion if you do. For more on Python typographical rules, see PEP 8.)

Votes + Comments
good help
Nice work!

Thank you! The typographical conventions are quite helpful as well.

Your gracious reply has helped me greatly. My problem was that, as before I stated, I was trying to call a function to a string, and that only worked if I did some really not-ok things with exec() and concatenation. Anyway, the reason I was using strings in the inventory instead of instantiations was because I wanted to be able to print out the name of the item. I didn't realize there was a __str__ constructor, this will be very useful for later projects and made for an excellent learning opportunity.

Once again, you have helped me greatly. Thank you.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.