Reputation: 11
I want to get a random object from a class, however, I have no idea how to do this. Here's my code:
class Pokemon:
def __init__(self, name, health, damage, move, move2) -> None:
self.name = name
self.health = health
self.damage = damage
self.move = move
self.move2 = move2
Venusaur = Pokemon('Venusaur', '1295', '124', 'slash', 'venomous spit')
Charizard = Pokemon('Charizard', '1260', '128', 'bite', 'fire breath')
Blastoise = Pokemon('Blastoise', '1320', '113', 'Stomp', 'water gun')
pokemon2 = Pokemon(random.choices(Venusaur,Blastoise,Charizard))
pokemon1 = input('Choose your pokemon\nVenusaur\nCharizard\nBlastoise\n\n')
print(pokemon2)
Upvotes: 1
Views: 54
Reputation: 7410
You can construct a list with the elements you want to choose from. In your case the Pokemons, and then run random.choice
on the list.
import random
mylist = [Venusaur, Blastoise, Charizard]
print(random.choice(mylist))
Upvotes: 1