목록 및 사전 장에서 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
계속 어떤 오류 라인에서 수행하려고합니다 :,363,210
경우 attributes.keys에 remove_selection() 및 remove_selection [1]> 0 :
사용자의 입력 선택은 제 사전 속성 키이고 있는지 확인 있는지 확인를 먼저 속성 선택 그들은 실제로 몇 가지 포인트의 큰 수 당신은 remove_selection
문자열의 두 번째 문자를 얻으려고 0
에 [mcve] 제공 제발 봐 원하는 것 같아요. –
추적을 게시 하시겠습니까? – juniorRubyist