# Question Use Python Please: Now we want a way to make our monsters fight! Before two monsters can fight, we need to give two new class methods that update their stats. Implement a method for “win_fight” and “lose_fight”. Win_fight should add 5 to the monster’s self.exp, and reset their current_hp to their max_hp. Lose_fight should also reset their hp, but only adds 1 Traceback (most recent call last): File "jailed_code", line 151, in <module> exec(x) File "<string>", line 1, in <module> NameError: name 'monster_fight' is not defined

RZAR4M The Asker · Computer Science

Now we want a way to make our monsters fight!

Before two monsters can fight, we need to give two new class methods that update their stats.

Implement a method for “win_fight” and “lose_fight”. Win_fight should add 5 to the monster’s self.exp, and reset their current_hp to their max_hp. Lose_fight should also reset their hp, but only adds 1 exp to self.exp.

Now write a function that takes two instances of the Monster class as its input and makes them “fight”. A fight goes as follows:

1. The monster entered as the first function parameter always goes first.
2. Each monster takes a turn using one attack move.
3. Loop over all attacks from most powerful to least powerful attack until there is a winner. If there is a tie in hit points for an attack select using alphabetical order.

An attack is always successful and decreases the opponent’s current_hp by the given number of points. Monsters continue taking turns until one of them reaches current_hp less than or equal to 0, at which point the win_fight and lose_fight methods should be invoked. Once the fight is complete, return the round number in which the fight ended, the monster that won and the list of attacks the winning monster used. If both monsters only have “wait” as an attack, return -1, None, None

Sample Input 1:

a = Monster("a", 9)
b = Monster("b", 9)
b.remove_attack("wait")
a.remove_attack("wait")
round1, winner, moves = monster_fight(a, b)
print(round1)
print(winner.name)
print(moves)

Sample Output 1:

3
a
['ice_storm', 'ice_storm', 'ice_storm']

Sample Input 2:

a = Monster("a", 6)
b = Monster("b", 2)
round1, winner, moves = monster_fight(a, b)
print(round1)
print(winner)
print(moves)
print(a.exp)

Sample Output 2:

-1
None
None
0

Sample Input 3:

a = Monster("a", 10)
b = Monster("b", 9)
round1, winner, moves = monster_fight(a, b)
print(round1)
print(winner.exp)
print(moves)
print(b.exp)

Sample Output 3:

4
5
['fire_storm', 'whirlwind', 'wait', 'fire_storm']
1

Sample Input 4:

a = Monster("a", 9)
b = Monster("b")
round1, winner, moves = monster_fight(a, b)
print(round1)
print(winner.name)
print(moves)

Sample Output 4:

4
b
['ice_storm', 'whirlwind', 'wait', 'ice_storm']

Sample Input 5:

a = Monster("a", 9)
b = Monster("b", 8)
b.remove_attack("wait")
round1, winner, moves = monster_fight(a, b)
print(round1)
print(winner.name)
print(moves)

Sample Output 5:

3
b
['double_hit', 'double_hit', 'double_hit']

Sample Input 6:

a = Monster("a", 11)
b = Monster("b", 10)
b.remove_attack("double_hit")
round1, winner, moves = monster_fight(a, b)
print(round1)
print(winner.name)
print(moves)

Sample Output 6:

5
a
['ice_storm', 'whirlwind', 'wait', 'ice_storm', 'whirlwind']

Sample Input 7:

a = Monster("a", 11)
b = Monster("b", 10)
b.remove_attack("double_hit")
round1, winner, moves = monster_fight(a, b)
print(round1)
print(winner.max_hp)
print(winner.current_hp)

Sample Output 7:

5
11
11

#code I have

class Monster():
def __init__(self, name, hp=20):
self.exp = 0
self.attacks = {'wait': 0}
self.name = name
self.known_attacks = {'sneak_attack': 1, 'slash': 2, 'ice_storm': 3, 'fire_storm': 3,
'whirlwind': 3, 'earthquake': 2, 'double_hit': 4, 'wait': 0}
self.current_hp = hp
self.max_hp = hp
self.type = 'normal'

if attack_name in self.known_attacks and attack_name not in self.attacks:
try:
assert(len(self.attacks) < 4)
self.attacks[attack_name] = self.known_attacks.get(attack_name)
return True
except:
#find the min value of self.attacks
minval = min(self.attacks.keys(), key=(lambda k: self.attacks[k]))
for keys, values in self.attacks.items():
if self.attacks[minval] == values and min(minval, keys) == keys:
minval = keys
del self.attacks[minval]
self.attacks[attack_name] = self.known_attacks.get(attack_name)
return True
else:
return False

def remove_attack(self, attack_name):
if attack_name in self.attacks.keys():
del self.attacks[attack_name]
if len(self.attacks) == 0:
self.attacks['wait'] = 0
return True
else:
return False
def win_fight(self):
self.exp += 5
self.current_hp = self.max_hp
def lose_fight(self):
self.exp += 1
self.current_hp = self.max_hp

def monster_fight(monster1, monster2):

M1round = 0
M2round = 0
monster2attackLis = []
monster1attackLis = []
winner = None
round = 0

for values in monster2.attacks.values():
monster2attackLis.append(values)
for values in monster1.attacks.values():
monster1attackLis.append(values)

monster2attackLis = sorted(monster2attackLis, reverse=True)
monster1attackLis = sorted(monster1attackLis, reverse=True)

M1attacknames = sorted(monster1.attacks.items(), key=operator.itemgetter(1), reverse=True)
M2attacknames = sorted(monster2.attacks.items(), key=operator.itemgetter(1), reverse=True)

winnerList = [] #list of the winners moves

index1 = 0
index2 = 0

if all(key == 'wait' for key in monster1.attacks.keys()) and all(key == 'wait' for key in monster1.attacks.keys()):
return (-1, None, None)

while(monster2.current_hp > 0):

try:
monster2.current_hp -= monster1attackLis[index1]
index1 += 1
M1round += 1

except IndexError:

index1 = 0

while(monster1.current_hp > 0):

try:
monster1.current_hp -= monster2attackLis[index2]
index2 += 1
M2round += 1

except IndexError:

index2 = 0

if(M1round == M2round):
winner = monster1
round = M1round
monster1.win_fight()
monster2.lose_fight()
index = 0
i = 0
while i < M1round:
try:
winnerList.append(M1attacknames[index][0])
index += 1
i+=1
except:
index = 0
continue

elif(M1round < M2round):
winner = monster1
monster1.win_fight()
monster2.lose_fight()
round = M1round
index = 0
i = 0
while i < round:
try:
winnerList.append(M1attacknames[index][0])
index += 1
i+=1
except:
index = 0
continue

elif(M2round < M1round):
winner = monster2
monster2.win_fight()
monster1.lose_fight()
round = M2round
index = 0
i = 0
while i < M2round:
try:
winnerList.append(M2attacknames[index][0])
index += 1
i += 1
except:
index = 0
continue

return (round, winner, winnerList)

How to fix this problem? Thank You

Transcribed Image Text: Traceback (most recent call last): File "jailed_code", line 151, in exec(x) File "", line 1, in NameError: name 'monster_fight' is not defined
More
Transcribed Image Text: Traceback (most recent call last): File "jailed_code", line 151, in exec(x) File "", line 1, in NameError: name 'monster_fight' is not defined