Python Forum
user input to select and print data from another class python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
user input to select and print data from another class python
#1
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)
Reply
#2
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
It's defined in my player class as player.getinventory()

Inventory = []
Reply
#4
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
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()
Reply
#6
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#7
Aha thanks man, you're a blessing.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to Randomly Print a Quote From a Text File When User Types a Command on Main Menu BillKochman 13 860 Apr-24-2024, 05:47 AM
Last Post: Bronjer
  Help with to check an Input list data with a data read from an external source sacharyya 3 414 Mar-09-2024, 12:33 PM
Last Post: Pedroski55
  manually input data jpatierno 0 346 Nov-10-2023, 02:32 AM
Last Post: jpatierno
  Input network device connection info from data file edroche3rd 6 1,061 Oct-12-2023, 02:18 AM
Last Post: edroche3rd
  WHILE LOOP NOT RETURNING USER INPUT AFTER ZerroDivisionError! HELP! ayodele_martins1 7 1,072 Oct-01-2023, 07:36 PM
Last Post: ayodele_martins1
Question in this code, I input Key_word, it can not find although all data was exact Help me! duchien04x4 3 1,050 Aug-31-2023, 05:36 PM
Last Post: deanhystad
  print(data) is suddenly invalid syntax db042190 6 1,211 Jun-14-2023, 02:55 PM
Last Post: deanhystad
  restrict user input to numerical values MCL169 2 918 Apr-08-2023, 05:40 PM
Last Post: MCL169
  class Update input (Gpio pin raspberry pi) caslor 2 798 Jan-30-2023, 08:05 PM
Last Post: caslor
  user input values into list of lists tauros73 3 1,075 Dec-29-2022, 05:54 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020