Python 3.x 代码中的字符串索引超出范围错误

Python 3.x 代码中的字符串索引超出范围错误,python-3.x,error-handling,Python 3.x,Error Handling,在列表和字典一章中,我正在编写一个简单的字符生成器代码,这是我在Python中面临的挑战之一。我得到以下错误:“字符串索引超出范围”,我很难找出原因 这是代码,包含错误的行以粗体显示在末尾: print("\nChallenge 2:") attributes = {"Pool":30, "S":["Strength",0], "H":["Health",0], "W": ["Wisdom",0],"D":["Dexterity",0]} print("You have {} points

在列表和字典一章中,我正在编写一个简单的字符生成器代码,这是我在Python中面临的挑战之一。我得到以下错误:“字符串索引超出范围”,我很难找出原因

这是代码,包含错误的行以粗体显示在末尾:

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()
续码

                    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
remove\u amount=input(“\n您希望从{}中删除多少点:”
.format(属性[remove_selection][0]))
如果删除\u amount.isdigit():
删除金额=整数(删除金额)
如果0<删除金额属性[“池”]:
打印(!!!您的池中只有{}个点要添加!!!”。格式(属性[“池]))
其他:
打印(!!!无效输入!!!)
其他:
打印(!!!无效输入!!!)
elif remove_selection[1]==0:
打印(!!!该属性没有任何要删除的点!!!)
其他:
打印(!!!未找到属性!!!)
其他:
打印(!!!您的任何属性中都没有要删除的点!!!)
打破
其他:
打破
我试图在错误行中执行的操作: 如果删除attributes.keys()中的选择并删除选择[1]>0:


首先,检查用户的输入选项是否在属性字典的键中,然后,确保他们选择的要从中删除点的属性选项的点数实际上大于0。

删除选择[1]

您尝试获取
remove\u selection
字符串中的第二个字母,而

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()
所以
remove\u选择的
是S、W、D或H-1个字母长

我想你想

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

请看提供一个。你能发布一个回溯吗?哇,我是个白痴。它需要是:attributes[remove_selection][1]
if remove_selection in attributes.keys() and attributes[remove_selection][1] > 0: