如何在迷你角色扮演中指定有限的属性点

2024-10-04 03:22:44 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在向道森学习python,为绝对初学者编程,并尝试第5章的一个任务,为角色扮演英雄分配属性点。有30点要花,我的代码工作得很好,到目前为止,但一旦所有的30点都花了,因为它一直超过零崩溃

当所有的分数都用完了,我怎么停止这个节目

代码如下:

points = 30


Att = [["Strength", 0] , ["Health" , 0] , ["Wisdom" , 0] , ["Dexterity" , 0]]

    choice = ""

    while choice != "0":

            print ("\nYou have" , points , "points remaining")

            print ("""

0 - Exit
1 - Show Stats
2 - Assign Strength
3 - Assign Health
4 - Assign Wisdom
5 - Assign Dexterity

""")

            choice = input("\n\nChoice: ")

        if choice == "1":

        print ("\n")
        print (Att [0][0] , Att [0][1])
        print (Att [1][0] , Att [1][1])
        print (Att [2][0] , Att [2][1])
        print (Att [3][0] , Att [3][1])


        elif choice == "2":

             s = int(input("\nAdd points to Strength: "))

            Att [0][1] = Att [0][1] + s

            points = points - s

        elif choice == "3":

            h = int(input("\nAdd points to Health: "))

            Att [1][1] = Att [1][1] + h

            points = points - h

        elif choice == "4":

            w = int(input("\nAdd points to Wisdom: "))

            Att [2][1] += w

            points -= w

        elif choice == "5":

            d = int(input("\nAdd points to Dexterity: "))

            Att [3][1] += d

            points -= d

        elif choice == "0":


            input("Press enter if sure you have finished: ")

Tags: to代码inputstrengthattpointsintdexterity
2条回答

当用户还有0分要分配时,选项3、4和5应该无效。因此,您可以在这3个条件下添加一个if条件,检查用户是否可以分配更多的点。例如:

elif choice == "2":
    if points > 0:    # Check if the player actually has the points to spend...
        s = int(input("\nAdd points to Strength: "))
        if s > points:    # Don't let the user allocate more points than he has left
            s = points
        Att [0][1] = Att [0][1] + s
        points = points - s
    else:
        print("No more points to allocate!")    # Your error message of choice

您将不得不使用与其他stat分配选项类似的代码。如果需要,还可以在外while循环中添加一个条件以缩短代码量,但是如果用户的点数降为零,则这将不允许用户查看他们的统计信息(选项1)

您只需在while循环中添加一个检查,并在循环体中添加一个条件,在所有点都用完时通知用户:

points = 30


Att = [["Strength", 0] , ["Health" , 0] , ["Wisdom" , 0] , ["Dexterity" , 0]]

    choice = ""

    while choice != "0" and points > 0:

            print ("\nYou have" , points , "points remaining")


        # ...
        # (code same as in question)


        elif choice == "0":


            input("Press enter if sure you have finished: ")

        # If points have gone below 0, notify user
        if points <= 0:

            print("You ran out of points!")

相关问题 更多 >