Let’s say I have a class for a game I’m making and I need to print the highest value out of five individuals. How would I do that? for example
JavaScript
x
8
1
class villains:
2
def __init__(self,health, damage):
3
self.health = first_name
4
self.damage = damage
5
6
v1 = villains(20,15)
7
v2 = villains(22,12)
8
How would I get it to print the highest value for health instead of just printing the individual values?
Advertisement
Answer
Iterate over each villan and check if it’s the largest
JavaScript
1
9
1
v1 = villains(20,15)
2
v2 = villains(22,12)
3
4
all_villans = [v1, v2]
5
largest_villan = all_villans[0]
6
for current_villan in all_villans[1:]:
7
if current_villan.health > largest_villan.health:
8
largest_villan = current_villan
9