1

I am doing a simple character creator code as one of my challenges in Python in the chapter on lists and dictionaries. I get the following error: "string index out of range" and I'm having trouble figuring out why.

Here is the code, the line containing the error is towards the end in bold:

print("\nChallenge 2:")

attributes = {"Pool":30, "S":["Strength",0], "H":["Health",0], "W":
["Wisdom",0],"D":["Dexterity",0]}

print("You have {} points to spend on either \n1) Strength\n2) Health\n3) Wisdom\n4) Dexterity"
  .format(attributes["Pool"]))

while True:

add_or_remove = input("\nPress A to add points from the pool to an attribute. Press R to remove points from an "
                    "attribute and add it back to the pool. Otherwise, press any key to quit: ").upper()

if add_or_remove == "A" or add_or_remove == "R":

    if add_or_remove == "A":

        while True:

            if attributes["Pool"] > 0:

                add_selection = input("\nPress S to add points to Strength. Press H to add points to Health. "
                                      "Press W to add points to Wisdom. Press D to add points to Dexterity. "
                                      "Otherwise, press ENTER to go back: ").upper()


                if add_selection in attributes.keys() and add_selection != "Pool":
                    amount = input(
                        "\nHow many points would you like to add to {}: ".format(attributes[add_selection][0]))
                    if amount.isdigit():
                        amount = int(amount)
                        if 0 < amount <= attributes["Pool"]:
                            attributes[add_selection][1] += amount
                            attributes["Pool"] -= amount
                            print("\nPoints have been added successfully.")
                            print("\nUpdated attributes list: \n", attributes.values())
                            break
                        elif amount > attributes["Pool"]:
                            print("!!! You only have {} points in your pool to add !!!".format(attributes["Pool"]))
                        else:
                            print("!!! Invalid Input !!!")
                    else:
                        print("!!! Invalid Input !!!")
                elif add_selection == "":
                    break
                else:
                    print("!!! No attribute found !!!")

            elif attributes["Pool"] == 0:
                print("!!! You don't have any points in your pool to add. You need to remove some points from your "
                      "attributes and return them to the pool !!!")
                break

    elif add_or_remove == "R":

        while True:

            if attributes["S"][1] + attributes["H"][1] + attributes["W"][1] + attributes["D"][1] > 0:

                remove_selection = input("\nPress S to remove points from Strength. Press H to remove points from Health."
                                     " Press W to remove points from Wisdom. Press D to remove points from Dexterity. "
                                     "\nOtherwise, press any key to go back:  ").upper()

location of error

                if remove_selection in attributes.keys() and remove_selection[1] > 0:

continued code

                    remove_amount = input("\nHow many points would you like to remove from {}: "
                                          .format(attributes[remove_selection][0]))

                    if remove_amount.isdigit():
                        remove_amount = int(remove_amount)
                        if 0 < remove_amount <= attributes[remove_selection][1]:
                            attributes[remove_selection][1] -= remove_amount
                            attributes["Pool"] += remove_amount
                            print("\nPoints have been removed successfully.")
                            print("\nUpdated attributes list: \n", attributes.values())
                            break
                        elif remove_amount > attributes["Pool"]:
                            print("!!! You only have {} points in your pool to add !!!".format(attributes["Pool"]))
                        else:
                            print("!!! Invalid Input !!!")
                    else:
                        print("!!! Invalid Input !!!")
                elif remove_selection[1] == 0:
                    print("!!! That attribute does not have any points to remove !!!")
                else:
                    print("!!! No attribute found !!!")
            else:
                print("!!! You don't have any points in any of your attributes to remove !!!")
                break
else:
    break

what I am trying to do in the error line: if remove_selection in attributes.keys() and remove_selection[1] > 0:

is first, check if the user's input choice is in the keys of the attributes dictionary and second, to make sure that the attribute choice they have selected to remove points from actually has some number of points greater than 0.

2
  • Please look at providing a minimal reproducible example. Commented Oct 26, 2017 at 17:38
  • Could you please post a traceback? Commented Oct 26, 2017 at 17:48

1 Answer 1

1

remove_selection[1]

You try to get the second letter in remove_selection string, while

remove_selection = input("\nPress S to remove points from Strength. Press H to remove points from Health."
                                     " Press W to remove points from Wisdom. Press D to remove points from Dexterity. "
                                     "\nOtherwise, press any key to go back:  ").upper()

so remove_selection is S, W, D or H - 1 letter long.

I guess you want to

if remove_selection in attributes.keys() and attributes[remove_selection][1] > 0:
Sign up to request clarification or add additional context in comments.

1 Comment

Wow I'm an idiot. It needed to be: attributes[remove_selection][1]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.