'Can I select a list to search by user inputted text such that the inputted text matches the variable name of the list

This is part of a "playground" program where I try out things as I am learning python, for data its all ad&d related because reasons.

Anyway I was looking for a way to improve code efficacy.

So I have these 4 lists (I realise a data frame would be more efficient and that will be the next bit of code I try)

rouge = [20, 20, 19, 19, 18, 18, 17, 17, 16,
         16, 15, 15, 14, 14, 13, 13, 12, 12, 11, 11]
priest = [20, 20, 20, 18, 18, 18, 16, 16, 16,
          14, 14, 14, 12, 12, 12, 10, 10, 10, 8, 8]
warrior = [20, 19, 18, 17, 16, 15, 14, 13,
           12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
wizard = [20, 20, 20, 19, 19, 19, 18, 18, 18,
          17, 17, 17, 16, 16, 16, 15, 15, 15, 14, 14]

Anyway my old, working, code would retrieve the THAC0 values, aka those in lists, based upon index value which the player would enter as "playerlevel".

I sought to improve it with this code (the old code is in a separate file and does not effect this, it worked as a bunch of nested if statements and took over 60 lines of code).

    playerclass = input("Please enter a class type: ").strip().lower()
    if not playerclass in possibleclasses: #a comparison to a list to prevent input of invalid data
        playerclass = input("Enter a valid class type: ").strip().lower()
    playerlevelinput = input("Please enter level: ")
    while not playerlevelinput.isdigit():
        playerlevelinput = input(
            "Error. Please input a level, a level must be an integer value: ")
    playerlevel = (int(playerlevelinput) - 1) #-1 due to index start at 0
    THAC0 = playerclass[playerlevel]  *# doesn't work*
    print(f"The character's THAC0 is {THAC0}")
    wait = input("Press any key to continue")

Now this part doesn't work, however I hope its clear what I tried to do, I wish to search in the list for which the variable name (of the list) matches the playerclass input variable; e.g, search in the wizard list if the input is "wizard".

I receive "IndexError: string index out of range" when I try this.



Solution 1:[1]

You can make a dictionary where keys are names of classes and the values are the lists:

# wizard, priest, warrior, rouge are the four lists from the question:
classes_dct = {
    "wizard": wizard,
    "priest": priest,
    "warrior": warrior,
    "rouge": rouge,
}


playerclass = input("Please enter a class type: ")
playerlevel = int(input("Please enter level: ")) - 1

if playerclass in classes_dct and playerlevel < len(classes_dct[playerclass]):
    THAC0 = classes_dct[playerclass][playerlevel]
    print(f"The character's THAC0 is {THAC0}")
else:
    print("Incorrect input.")

Prints (for example):

Please enter a class type: wizard
Please enter level: 3
The character's THAC0 is 20

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Andrej Kesely