0

Scenario: Making a simple strategy RPG in the console. The user is asked to select a race for their two starting characters. I'm trying to learn this process iteratively and learn how to use the (or so I thought) simple package; pyinputPlus.

There are several races, whose "stats" are placed into a dictionary. (Yes, I know I will use a database in the future; baby steps.) The list of the available races is a list, that houses the dictionaries. (Please tell me if this even remotely useful, for me - it helps with readability.)

import pyinputplus as pyip

# Basic, "hard-coded" version of the menu.
# Just to make sure I know how to use "inputMenu" at a basic level.
choiceone = pyip.inputMenu(['Elf', 'Human', 'Orc'])
print(choiceone)

# Replacing hard-coded list with a global list.
racelist = ['Elf', 'Gnome', 'Human', 'Orc']
choicetwo = pyip.inputMenu(racelist)
print(choicetwo)

# Using dictionaries to reference the difference between "singular" and "plural"
# (this is where the stat blocks will go in future iterations.)
DWARVES = {'singular': 'Dwarf', 'plural': 'Dwarves'}
ELVES = {'singular': 'Elf', 'plural': 'Elves'}
GNOMES = {'singular': 'Gnome', 'plural': 'Gnomes'}
GOBLINS = {'singular': 'Goblin', 'plural': 'Goblins'}
HUMANS = {'singular': 'Human', 'plural': 'Humans'}
LIZARDFOLK = {'singular': 'Reptilian', 'plural': 'Lizardfolk'}
ORCS = {'singular': 'Orc', 'plural': 'Orcs'}

# This list only here for decorative/informational purposes to make it clearer to someone reading
# this code the correlation the dictionaries are offering.
standard_races_singular = ['Dwarf', 'Elf', 'Gnome', 'Goblin', 'Human', 'Reptilian', 'Orc']

# This list is a list of the earlier dictionaries.  I know I can combine these as an actual list of
# dictionaries, but, readability would suffer for me.
standard_races_dict_ref = [DWARVES, ELVES, GNOMES, GOBLINS, HUMANS, LIZARDFOLK, ORCS]

def display_standard_race_options(options):
    '''
        This function is designed to show what the standard race options are.
        This should loop over the Standard_Races array, and display the SINGULAR version of the Race's collective name.
    '''
    for r in options:
        print(r['singular'])

# This function runs just fine.  And successfully displays, one line at a time, the options.
display_standard_race_options(standard_races_dict_ref)

# Here I get a traceback error "list indices must be integers or slices, not str"
choicethree = pyip.inputMenu(standard_races_dict_ref['singular'])
print(choicethree)

ChoiceOne works as expected. The hard-coded values are presented to the screen, and as long as you type one of those options - the program continues. ChoiceTwo works exactly the same, even successfully understanding the list I referenced. ChoiceThree... That's where I run into problems.

Traceback (most recent call last): File "C:~~~\tryinputplus.py", line 43, in choicethree = pyip.inputMenu(standard_races_dict_ref['singular']) TypeError: list indices must be integers or slices, not str

Is what I am trying to do even possible? What do I need to do to successfully "find" the singular, while still having the result set the final value to one of the actual options. I think I'm making a leap in logic, but failing to see it.

  • Some other questions that I read hoping to find answers, but, didn't get me there: https://stackoverflow.com/questions/70428176/validation-exception-when-using-inputmenu-pyinputplus (it's more about numbers, my problem is about the strings) https://stackoverflow.com/questions/44372559/typeerror-list-indices-must-be-integers-or-slices-not-str-convert-the-charart (same deal.) https://stackoverflow.com/questions/65545430/displaying-dict-keys-from-users-input (maybe this is how I should be constructing the options? Making a numeric menu?) – C. Michael Dec 26 '21 at 16:44

0 Answers0