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
class villains: def __init__(self,health, damage): self.health = first_name self.damage = damage v1 = villains(20,15) v2 = villains(22,12)
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
v1 = villains(20,15) v2 = villains(22,12) all_villans = [v1, v2] largest_villan = all_villans[0] for current_villan in all_villans[1:]: if current_villan.health > largest_villan.health: largest_villan = current_villan