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
#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


Messages In This Thread
RE: user input to select and print data from another class python - by ichabod801 - Aug-30-2018, 12:53 PM

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 1,202 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 522 Mar-09-2024, 12:33 PM
Last Post: Pedroski55
  manually input data jpatierno 0 377 Nov-10-2023, 02:32 AM
Last Post: jpatierno
  Input network device connection info from data file edroche3rd 6 1,173 Oct-12-2023, 02:18 AM
Last Post: edroche3rd
  WHILE LOOP NOT RETURNING USER INPUT AFTER ZerroDivisionError! HELP! ayodele_martins1 7 1,138 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,125 Aug-31-2023, 05:36 PM
Last Post: deanhystad
  print(data) is suddenly invalid syntax db042190 6 1,284 Jun-14-2023, 02:55 PM
Last Post: deanhystad
  restrict user input to numerical values MCL169 2 988 Apr-08-2023, 05:40 PM
Last Post: MCL169
  class Update input (Gpio pin raspberry pi) caslor 2 860 Jan-30-2023, 08:05 PM
Last Post: caslor
  user input values into list of lists tauros73 3 1,126 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