我正在做一个简单的角色创建者代码,作为我在列表和词典章节中的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()
错误的位置
if remove_selection in attributes.keys() and remove_selection[1] > 0:
继续代码
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
我在错误行中尝试做的事情: 如果attributes.keys()和remove_selection [1]&gt;中的remove_selection 0:
首先,检查用户的输入选择是否在属性字典的键中,然后确保他们选择的用于删除点的属性选择实际上具有大于0的某些点数。
答案 0 :(得分:1)
remove_selection[1]
您尝试在remove_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_selection
是S,W,D或H - 1个字母长。
我想你想要
if remove_selection in attributes.keys() and attributes[remove_selection][1] > 0: