Dec-08-2020, 09:23 PM
Hello everyone. I can't write a function that generates random business cards in a python's faker. The function is to assume two parameters: the type of the business card (business or home) and the quantity. I provide two codes below. The first to work and to generate one "human":
Broken code below:
from faker import Faker fake = Faker("en_US") class BaseContact: def __init__(self, first_name, last_name, email_address,tel_priv): self.first_name = first_name self.last_name = last_name self.email_address = email_address self.tel_priv = tel_priv def __str__(self): return f"{self.first_name} {self.last_name}, {self.email_address}, {self.occupation}, {self.company}" def __repr__(self): return f"Card(first_name={self.first_name} last_name={self.last_name}, adres email={self.email_address})" def contact(self): return f"Choose home phone: {self.tel_priv} and call to {self.first_name} {self.last_name} " def workcontact(self): return f"Choose work phone: {self.tel_work} and call to {self.first_name} {self.last_name}" @property def label_lenght(self): return sum([len(self.first_name), len(self.last_name),+1]) class BusinessContact(BaseContact): def __init__(self, tel_work, company, occupation, *args, **kwargs): super().__init__(*args, **kwargs) self.tel_work = tel_work self.company = company self.occupation = occupation human_1 = BusinessContact(first_name=fake.first_name(), last_name=fake.last_name(), company=fake.company(), occupation=fake.job(), email_address=fake.email(), tel_priv=fake.phone_number(), tel_work=fake.phone_number()) print(human_1) print(human_1.contact()) print(human_1.workcontact()) print(human_1.label_lenght) print()Now.How to create function and generate random home or business cards?
Broken code below:
def create_contacts(kind, how_many): contacts = [] for i in range(how_many): if kind == 'b': how_many.append(BusinessContact) elif kind == 'd': how_many.append(BaseContact) return contacts if __name__ == "__main__": kind = input("select the type of business card: b - business, h - home: ") how_many = int(input('please select number of cards ')) contacts = create_contacts(kind, how_many) print(contacts)