Page 1 of 1

print("\n\nc. Check if each variable in the list is a class or instance variable.") variables = ['_ID', 'ID', 'category'

Posted: Mon Jun 06, 2022 5:07 pm
by answerhappygod
Print N Nc Check If Each Variable In The List Is A Class Or Instance Variable Variables Id Id Category 1
Print N Nc Check If Each Variable In The List Is A Class Or Instance Variable Variables Id Id Category 1 (40.72 KiB) Viewed 27 times
Print N Nc Check If Each Variable In The List Is A Class Or Instance Variable Variables Id Id Category 2
Print N Nc Check If Each Variable In The List Is A Class Or Instance Variable Variables Id Id Category 2 (174.82 KiB) Viewed 27 times
CODE TO COPY:
class Fighter:
category = "Fighter" # class attribute/variable
_ID = 0 # this class variable is PROTECTED with one
underscore
def __init__(self, hit_points=100, damage=10):
Fighter._ID += 1 # increment the protected class variable
self.ID = Fighter._ID # this instance variable is public
self.fullID = "Fighter:" + ("%04d" % self.ID)
self.name = "Anonymous Fighter"
self.hit_points = hit_points # the health of the fighter
measured in hit points.
self.damage = damage # the damage dealt by the fighter when it
attacks an enemy
self.success_rate = 50 # probability of an attack being
successful
# add code to replace 50 with a random integer from 25 to 75
self.dead = (self.hit_points == 0)
def attack(self, target):
if self.dead: # cannot attack if dead
return
if random.randint(1, 100) <= self.success_rate: # probability
attack is successful
# the target takes damage here
target.hit_points = max(target.hit_points - self.damage, 0) #
min is zero
target.dead = (target.hit_points == 0) # update death status of
target
# add code to increase success_rate by 2. Use min() to assure
this does not exceed 100%
# add code here
def __str__(self):
if self.dead:
return ANSI.RED + ANSI.BOLD + "DEAD" + ' ' * 8 + ANSI.END
else:
return str("%04d" % self.hit_points)
def regenerate(self):
# Reset the hit points to 100 here.
# Reset the dead attribute to False
pass
def death_match(self, other):
print((ANSI.BOLD + "%-12s|%-12s" + ANSI.END) % (self.name,
other.name))
print("%-12s|%-12s" % (self, other))
while not (self.dead or other.dead):
# add two lines of code so that each combatant attacks the
opponent
time.sleep(0.2)
print("%-12s|%-12s" % (self, other))
victor = ""
if self.dead:
victor = other
else:
victor = self
print("\nThe victor of this death match is " + victor.name)
print("The victor's success rate is now ",
victor.success_rate)
print("\n\nc. Check if each variable in the list is a class or instance variable.") variables = ['_ID', 'ID', 'category', 'name', 'hit_points', 'damage', 'nope'] for var in variables: # add if/elif/else blocks here.
4 25 26 27 28 29 20132334353578 39 40 41 2 3 4 45 46 7 8 49 505123455 568596051234567860701234567 78 79 80 24 36 42 43 44 47 48 ##################################### ##################* # FIGHTER SUPER CLASS ###################################################################### class Fighter: category= "Fighter" # class attribute/variable _ID = 0 # this class variable is PROTECTED with one underscore definit__(self, hit_points=100, damage=10): Fighter. ID += 1 # increment the protected class variable self.ID = Fighter._ID # this instance variable is public self.fullID = "Fighter:" + ("%04d" % self.ID) self.name = "Anonymous Fighter" self.hit_points = hit_points # the health of the fighter measured in hit points. self.damage damage # the damage dealt by the fighter when it attacks an enemy self.success_rate= 50 # probability of an attack being successful # add code to replace 50 with a random integer from 25 to 75 self.dead (self.hit_points == 0) def attack(self, target): if self.dead: # cannot attack if dead return if random.randint(1, 100) <= self.success_rate: # probability attack is successful # the target takes damage here target.hit_points = max(target.hit_points – self.damage, 0) # min is zero target.dead (target.hit_points == 0) # update death status of target # add code to increase success_rate by 2. Use min() to assure this does not exceed 100% # add code here def_str_(self): if self.dead: return ANSI.RED + ANSI. BOLD + "DEAD" + * 8+ ANSI.END else: return str("%04d" % self.hit_points) def regenerate(self): # Reset the hit points to 100 here. # Reset the dead attribute to False pass def death_match(self, other): print ((ANSI.BOLD + "%-12s | %-12s" + ANSI.END) % (self.name, other.name)) print("%-12s | %-12s" % (self, other)) while not (self.dead or other.dead): # add two lines of code so that each combatant attacks the opponent time.sleep(0.2) print("%-12s | %-12s" % (self, other)) victor=" if self.dead: victor other else: victor = self print("\nThe victor of this death match is " + victor.name) print("The victor's success rate is now ", victor.success_rate)