4つの異なるキャラクター属性に30ポイントを割り当ててキャラクタープロファイルを作成する、簡単なプログラミング演習(まだ新しい)を完了しています。プログラムの機能は次のとおりです。現在のプロファイルの表示、新しいプロファイルの作成、または既存のプロファイルの変更。最初と2番目の機能は正常に機能しますが、最後の機能には問題があります。プログラムは、ネストされたリストアイテム(属性+割り当てられたスコア)を解凍し、新しいスコアを要求し、古いものと新しいものの差を取り、数を変更することを目的としています。それに応じてプール内の利用可能なポイントの。最後に、リストの位置0に新しいエントリ(属性+新しく割り当てられたスコア)を追加してから、位置1のエントリを削除します。これは、この属性の古いエントリである必要があります。リストをループして完了します。ただし、コードを実行すると、機能しないことがわかります。以下の完全なコードを参照してください。
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.")
注:変更を実行するには、最初にプロファイルを作成する必要があります。
よろしくお願いします!!