mercredi 16 mars 2022

Create 100 Participants in a class without declaring them one by one [duplicate]

My plan is to allocate 100 Participants with random attributes to a class. What is the best way to do that without having to declare them one by one like this:

p1 = Participant() 
p2 = Participant() 
p3 = Participant() etc.

Class definition:

import names
import random

status_list = ["single", "married", "widowed", "complicated"]

class Paricipant():
    def __init__(self, name= None, age = None, status = None):
        if name is None:
            self.name = names.get_first_name()
        if age is None:
            self.age = random.randint(22, 50)
        if status is None:
            self.status = random.choice(status_list)

    def return_all(self):
        print(self.name)
        print(self.age)
        print(self.status)



Aucun commentaire:

Enregistrer un commentaire