Python Forum
Generating random business cards
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Generating random business cards
#1
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":
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)
Reply
#2
Error:
select the type of business card: b - business, h - home: b
please select number of cards 2
Traceback (most recent call last):
  File "main.py", line 16, in <module>
    contacts = create_contacts(kind, how_many)
  File "main.py", line 7, in create_contacts
    how_many.append(BusinessContact)
AttributeError: 'int' object has no attribute 'append'
Reply
#3
how_many is an int - number of business cards you take it as input from the user.
it does not have .append() method (that would be a list). I guess you want to use contacts.append()
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Classes, OOP, building deck of 52 playing cards Drone4four 9 3,818 Jan-19-2022, 02:53 PM
Last Post: Drone4four
  generating random string unique forever Skaperen 5 2,286 Aug-20-2021, 07:15 AM
Last Post: Gribouillis
  Appropriate data-structure / design for business-day relations (week/month-wise) sx999 2 2,754 Apr-23-2021, 08:09 AM
Last Post: sx999
  Simple cards game blackpanda 3 4,184 Apr-10-2020, 08:46 PM
Last Post: TomToad
  SAP Business Objects Query SQL AsaithambiThiyagarajan 0 2,516 Jan-08-2020, 12:14 AM
Last Post: AsaithambiThiyagarajan
  python program on cards marc237 3 2,725 Apr-27-2019, 09:26 PM
Last Post: ichabod801
  Business formulas more help needed!! jy0013 4 3,376 Aug-29-2017, 12:21 AM
Last Post: jy0013

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020