Python 3:我如何在每次 class 添加后停止 class 中的 while 循环?

Python 3: How Do I Stop A while-loop In class From Running After Every class I Added?

我有一个 while-loop 嵌套在 Class "Function/Block" 中。我做了研究,如果你想在 class 中放置一个计数器,你应该放置 self.Counter (for example)。在此之前,我使用 break 函数来停止我的 while-loop。它可能位于错误的位置或未正确设置,但是,即使它是 False,它仍会继续。这是代码:

class Class:
    def __init__(self, Health, Attack, Defense):
        self.Health = Health
        self.Attack = Attack
        self.Defense = Defense
        self.Role = []
        self.Move = []
        self.RoleCount = 1
        while(self.RoleCount > 0):
            Input = input("What role do you want to be?\nChoices:\n1. Mage\n2. Warrior\n3. Archer\n4. Gunner\n")
            if(Input.lower() in ['mage', 'warrior', 'archer', 'gunner']):
                print("Your role is: %s" % (Input.upper()[0]+Input.lower()[1:]))
                self.Role.append(Input.upper()[0]+Input.lower()[1:])
                self.RoleCount -= 1
            else:
                print("That's not a role! Try again.")
Mage = Class(40, 20, 7)
Warrior = Class(60, 9, 10)
Archer = Class(50, 12, 18)
Gunner = Class(55, 16, 12)

您可能需要 运行 这样您才能看到我遇到的问题,但是,每当我 运行 这个时,while-loop 即使在 False.如果您能告诉我为什么会发生这种情况以及如何解决它,我将不胜感激:)。谢谢!

此外,这是我在尝试之前的代码之前尝试的 break 版本。它仍然没有阻止 while-loop.

class Class:
    def __init__(self, Health, Attack, Defense):
        self.Health = Health
        self.Attack = Attack
        self.Defense = Defense
        self.Role = []
        self.Move = []
        while(True):
            Input = input("What role do you want to be?\nChoices:\n1. Mage\n2. Warrior\n3. Archer\n4. Gunner\n")
            if(Input.lower() in ['mage', 'warrior', 'archer', 'gunner']):
                print("Your role is: %s" % (Input.upper()[0]+Input.lower()[1:]))
                self.Role.append(Input.upper()[0]+Input.lower()[1:])
                break
            else:
                print("That's not a role! Try again.")
Mage = Class(40, 20, 7)
Warrior = Class(60, 9, 10)
Archer = Class(50, 12, 18)
Gunner = Class(55, 16, 12)

我假设问题出在 while-loop 的定位上,但是,我真的不知道。任何澄清都会有所帮助!

更新信息 1:它为我制作的所有 "classes" 保持 运行ning,但是,我需要那些 classes 来验证每个的统计信息角色,即 'Mage'、'Warrior' 等。如何使 while 循环 运行 一次,即使它们是四个 类?

类:

Mage = Class(40, 20, 7)
Warrior = Class(60, 9, 10)
Archer = Class(50, 12, 18)
Gunner = Class(55, 16, 12)

这不是无限循环;一切正常。你只是调用构造函数四次,所以它会提示你四次。

听起来您想执行以下操作:

Given some user input, if the input corresponds to a valid role, then return an instance of that role with various pre-defined stats. Otherwise, if the input is invalid, then loop until the input is valid.

在这种情况下,我们可以执行以下操作:

class Role:
    def __init__(self, health, attack, defense):
        self.health = health
        self.attack = attack
        self.defense = defense
        self.move = []

def choose_role():
    role_stats = {
        "mage": (40, 20, 7),
        "warrior": (60, 9, 10),
        "archer": (50, 12, 18),
        "gunner": (55, 16, 12)
    }
    menu = "\n".join([
        "What role do you want to be?",
        "Choices:",
        "1. Mage",
        "2. Warrior",
        "3. Archer",
        "4. Gunner",
        ""
    ])

    while True:
        choice = input(menu).lower()
        if choice in role_stats:
            stats = role_stats[choice]
            print("Your role is: %s" % choice.capitalize())
            return Role(*stats)
        else:
            print("That's not a role! Try again.")

role = choose_role()
print("Health: %d, Attack: %d, Defense: %d" %
      (role.health, role.attack, role.defense))

如果您希望用户选择 class,那么我已经对您的代码进行了如下调整!

class Role:
    def __init__(self, Health, Attack, Defense):
        self.Health = Health
        self.Attack = Attack
        self.Defense = Defense
        self.Move = []

def chooseClass():
    Roles = {
               'mage':lambda:Role(40, 20, 7),
               'warrior':lambda:Role(60, 9, 10),
               'archer':lambda:Role(50, 12, 18),
               'gunner':lambda:Role(55, 16, 12)
               }

    while(True):
        Input = input("What role do you want to be?\nChoices:\n1. Mage\n2. Warrior\n3. Archer\n4. Gunner\n")
        if(Input.lower() in Roles):
            print("Your role is: %s" % (Input.upper()[0]+Input.lower()[1:]))
            break
        else:
            print("That's not a role! Try again.")
chooseClass()