Python Forum

Full Version: user input to select and print data from another class python
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
This is a snippet of my playercharacter class, specifically the inventory which later calls Weapon which is another class with a list of weapons and individual stats. I am attempting to have user input select one of the weapons from the inventory specifically, then print out the details of the weapon, but am having difficulties coming up with the logic for it. Can anyone assist me in doing so?

def getinventory(self): 
    for object in self.inventory:
        print object.getname()

        choice = raw_input("""Do you wish to look at any of the items?
""")
        if choice == 'yes' or choice == 'y':
            choice = raw_input("""Which item?
""")
            if choice == object.name in self.inventory:
                object.__str__(choice)
It's kind of hard to answer, because it is unclear what inventory is. If I was to make an inventory, I would use a dictionary:

inventory = {'cutlass': {'cost': 50, 'min_st': 10, 'damage': (2, -2)},
    {'battle axe': {'cost': 130, 'min_st': 15, 'damage': (3, 0)},
    {'halberd': {'cost': 70, 'min_st': 13, 'damage': (2, 0)}}

buy = input('Would you like to buy a weapon? ')
if buy.lower() in ('yes', 'y'):
    item = input('What would you like to buy? ')
    if item.lower() in inventory:
        wallet -= inventory[item]['cost']
        stuff.append(item)
    else:
        print('I don't have that for sale.')
Of course, you'd want to put that in a loop so they could buy multiple items. Note if item.lower() in inventory:. That is True if the text entered by the user (lower cased) matches one of the keys of inventory. So in my limited example above, it would only be True for cutlass, halberd, or battle axe, ignoring capitalization. Then it would put that key into the player's equipment (stuff), and you could later get the necessary stats like min_st and damage using the key.

Of course, this may not work for your inventory. Again, I can't answer the question fully without knowing how inventory is set up.
It's defined in my player class as player.getinventory()

Inventory = []
So, inventory is just a list of items? Then loop through them and print them out with a number, and have the user input a number to get the item:

for item_index, item in enumerate(inventory):
    print('{}: {}'.format(item_index, item))
print()
choice = int(input('Which item would you like to view? '))
chosen_item = inventory[choice]
Enumerate returns tuples of the index of an item and the item itself. You might want bells and whistles for this, like handling non-integer input or integers that aren't valid indexes for inventory.
Well, check it out. What I want is something like

Item = raw_input("choose an item to view details of")
Then I need to check if the item is in player.inventory, then print details with item.getdetails().
I'm not sure if you answered my question or if I just didn't catch it exactly.
Player is a class, weapon is a class. Roughly like
user input == weapon.name and in player.inventory:
Weapon.getdetails()
Okay, then how about:

choice = input('Choose an item to view the details of: ').lower()
for item in self.inventory:
    if choice == item.name:
        item.getdetails()
It would be more efficient if you stored it as a dictionary rather than a list. Then you wouldn't need the loop. Also, your use of raw_input makes me think you are using Python 2.x. You should switch to Python 3.x.
Aha thanks man, you're a blessing.