BradTheBrutalitist BradTheBrutalitist - 5 months ago 9
Python Question

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

I have a

while-loop
nested in a
Class "Function/Block"
. I did research and your are suppose to put
self.Counter (for example)
, if you want to put a counter in class. Before this I used a
break
function to stop my
while-loop
. It might be in the wrong place or it wasn't set up properly but, even though it's
False
it keeps continuing. Here is the code:

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)


You may need to run this so you can see the problem I am seeing but, whenever I run this, the
while-loop
keeps going even after it's
False
. If you can tell me why this is occurring and how to fix it I would appreciate it :). Thanks!

Also, here is the
break
version I tried before I tried the previous code. It still didn't stop the
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)


I'm assuming the problem is the positioning of the
while-loop
but, I truly don't know. Any clarification would be helpful!

Update Info 1: It keeps running for all of the "classes" I made but, I need those classes for verification of stats for each role i.e. 'Mage', 'Warrior', etc. How do I make the while loop run once even though their are four Classes?

Classes:

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

Answer

If you want the user to chose a class then I have adapted your code as follows!

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()