-1

我正在完成一个简单的编程练习(我还是新手),通过将 30 个点分配给 4 个不同的角色属性来创建角色配置文件。程序功能包括:显示当前配置文件、创建新配置文件或更改现有配置文件。第一个和第二个功能工作正常,但最后一个有问题:程序旨在解压缩嵌套列表项(属性+分配的分数),要求新的分数,取新旧之间的差值并更改数字池中相应的可用点数。最后,在列表的位置 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.")

注意:您需要先创建配置文件才能运行更改。

在此先感谢您的帮助!!

4

1 回答 1

0

正如对您问题的评论所表明的那样,您没有以最好的方式提出您的问题。但是,我知道它有什么问题。我可以向您展示如何修复您当前的代码,但事实是修复它的最佳方法是完全重写它。当你这样做时,你应该采取以下策略:

  1. 把你的代码分成几部分。在这种情况下,我建议您创建几个不同的函数。可以调用一个main_loop,并且将包含用于循环菜单的逻辑。它不包含任何用于更新或显示配置文件的代码。相反,它会调用其他函数display_profilebuild_profileamend_profile。这些函数将接受诸如 、 和 之类的变量optionsprofilepoints返回诸如options和之类的值points。这将极大地简化您的代码,并使测试和调试变得更加容易。main_loop这是一个可能看起来像的示例:

    def main_loop():
        options = ["Strength", "Health", "Wisdom", "Dexterity"]
        profile = []
        points = 30
    
        choice = None
        while choice != "0":
            print(menu_string)   #define menu_string elsewhere
            choice = input("Please choose an option: ")
            print()
    
            if choice == "0":
                print("Good bye.")
            elif choice == "1":
                display_profile(profile)
            elif choice == "2":
                profile, points = build_profile(options, profile, points)
            elif choice == "3":
                profile, points = amend_profile(profile, points)
            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.")
    

    看看这有多好?现在您所要做的就是定义其他函数。就像是...

        def build_profile(options, profile, points):
            # insert logic here
            return profile, points
    

    这种方法的另一个优点是现在您可以单独测试这些功能,而无需运行整个程序。

  2. 使用正确的习语进行列表修改。在迭代列表时修改列表需要特别小心,并且在某些情况下(例如,当您通过删除或添加已经迭代的项目来更改列表的长度时)它根本不起作用。有很多方法可以做您尝试做的事情profile,但对于初学者来说,我会推荐一些更简单的方法:只需创建一个新列表!然后返回该列表。所以在你的amend_profile函数中,做这样的事情:

        def amend_profile(profile, points):
            # other code ...
            new_profile = []
            for item in profile:
                attribute, points_aloc = item
                # other code ...
                new_proflie.append(entry)
            # other code ...
    
            return new_profile, points
    

    另请注意,这是您的主要错误之一;你创建了一个entry包含(item, point_new)而不是,所以你的新元组里面(attribute, point_new)有一个元组,而不是预期的一个单独的字符串。itemattribute

于 2012-04-21T16:37:57.087 回答