4 가지 다른 특성에 30 포인트를 할당하여 문자 프로필을 만드는 간단한 프로그래밍 연습을 마친다. 프로그램 기능은 현재 프로필 표시, 새 프로필 만들기 또는 기존 프로필 변경입니다. 첫 번째와 두 번째 기능은 정상적으로 작동하지만 마지막에는 문제가 있습니다. 프로그램은 중첩 목록 항목 (속성 + 할당 된 점수)을 풀고 새 점수를 요청하며 이전 항목과 새 항목의 차이를 가져 와서 번호를 변경합니다. 그에 따라 풀의 사용 가능한 포인트를 계산합니다. 마지막으로 위치 0에있는 목록 (속성 + 새로 할당 된 점수)에 새 항목을 추가 한 다음 위치 1의 항목을 삭제합니다.이 항목은이 속성에 대한 이전 항목이어야합니다. 루프를 통해 목록을 완성합니다. 그러나 일단 코드를 실행하면 작동하지 않을 것입니다. 전체 코드 아래를 참조하십시오.Python 중첩 목록 - 개별 항목 바꾸기 및 바꾸기
options = ["Strength", "Health", "Wisdom", "Dexterity"]
profile = []
points = 30
choice = None
while choice != "0":
print(
"""
CHARACTER CREATOR PROGRAM
0 - Exit
1 - See current profile
2 - Build new profile
3 - Amend existing profile
"""
)
choice = input("Please choose an option: ")
print()
if choice == "0":
print("Good bye.")
elif choice == "1":
for item in profile:
print(item)
input("\nPress the enter key to continue.")
elif choice == "2":
print("You can now equip your character with attributes for your adventures.")
print("You have",points,"points to spent.")
print("Now configure your character: \n")
#Run the point allocation loop
for item in options:
point_aloc = int(input("Please enter points for " + str(item) + ":"))
if point_aloc <= points:
entry = item, point_aloc
profile.append(entry)
points = points - point_aloc
print("\nYour current choice looks like this: ")
print(profile)
input("\nPress the enter key to continue.")
else:
print("Sorry, you can only allocate", points," more points!")
print("\nYour current choice looks like this: ")
print(profile)
input("\nPress the enter key to continue.")
print("\nWell done, you have configured your character as follows: ")
for item in profile:
print(item)
input("Press the enter key to continue.")
elif choice == "3":
print("This is your current character profile:\n")
for item in profile:
print(item)
print("\nYou can change the point allocation for each attribute.")
for item in profile:
point_new = int(input("Please enter new points for " + str(item) + ":"))
attribute, points_aloc = item
diff = points_aloc - point_new
if diff >0:
points += diff
print("Your point allocation has changed by", -diff,"points.")
print(diff,"points have just been added to the pool.")
print("The pool now contains", points,"points.")
entry = item, point_new
profile.insert(0, entry)
del profile[1]
input("Press the enter key to continue.\n")
elif diff <0 and points - diff >=0:
points += diff
print("Your point allocation has changed by", -diff,"points.")
print(-diff,"points have just been taken from the pool.")
print("The pool now contains", points,"points.")
entry = item, point_new
profile.insert(0, entry)
del profile[1]
input("Press the enter key to continue.\n")
elif diff <0 and points - diff <=0:
print("Sorry, but you don't have enough points in the pool!")
input("Press the enter key to continue.\n")
else:
print("Sorry, but this is not a valid choice!")
input("Press the enter key to continue.\n")
input("\n\nPress the enter key to exit.")
참고 : 변경 사항을 적용하려면 먼저 프로필을 만들어야합니다.
미리 도움을 청하십시오!
오신 것을 환영합니다 스택 오버플로.질문을 명확히 해 주시겠습니까? 여기에는 [짧고, 독립적 인, 올바른 예] (http://sscce.org/)가 포함되어야합니다. (오류 메시지 포함!) 및 [시도한 내용] (http://mattgemmell.com/2008/12/08/what-have-you-tried/)에 대한 설명이 포함되어 있습니다. 문제를 해결할 수 있습니다. – Ben
또한 예상되는 출력 및 출력을 포함해야합니다. –