mercredi 26 février 2020

Can you randomly pick an object from a class in python? [duplicate]

I would like to pick a player randomly from one of the objects I defined. This is what I have so far, I couldn't figure out a way to implement choice.

class Person(object):
    def __init__(self, name, gender, birthday,i):
        self.i = i
        self.name = name 
        self.gender = gender
        self.birthday = birthday
        self.job = None
    def get_job(self, job):
        self.job = job
    def quit_job(self):
        self.job = None
    def get_id(self,i):
        self.i = i


andrew = Person("A", "Male", "01-01-1995",1)
andrew.get_job("Software Engineer")
#andrew.quit_job()
burcu = Person("B","Female", "01-01-1992",2)
burcu.get_job("Teaching Assistant")

#persons = [Person.i for i in range(1,2)] # this does not work obviously

I figured maybe I can get an id for each one, put them in a list to pick a random item but didn't have any success.




Aucun commentaire:

Enregistrer un commentaire