Home > Back-end >  how to create multiple object in list using loop
how to create multiple object in list using loop

Time:01-03

So, I want to create different objects every time loops run, my object is [name, age, dob] which is appended in an empty list

data = []

I am using class

class PersonsData(object):
    # Object constructor
    def __init__(self):
        print("Person Data")
        self.name = ''
        self.age = 0
        self.doB = 0

    # Input Method
    def enter_data(self):
        size = int(input("Enter the number of data"))
        for i in range(size):
            self.name = str(input("Enter Your Name"   " ").upper())

            try:
                self.age = int(input("Enter Your Age"   " "))
            except:
                print("\n**Enter value in Number**")
                self.age = int(input("Enter Your Age"   " "))

            self.doB = (input("Enter Your DOB"   " "))
            print("\n")

    # Display Method
    def display(self):
        print("Name:", self.name)
        print("Age:", self.age)
        print("DOB:", self.doB)

the problem is instead of creating new object its just overwritting other, so ho I can create new object

my other half of the code

while True:
    print()
    print("""
    1.Add New Detail
    2.Display Detail
    3.Quit
    """)
    choice = int(input("Enter Choice:"   " "))
    if choice == 1:
        info = PersonsData()
        info.enter_data()
        print(info.name)
        data.append(info)
        print(data)

    elif choice == 2:
        for i in data:
            print("--------------------------------")
            i.display()
            print("--------------------------------")

    elif choice == 3:
        quit()

    else:
        print("Invalid choice")

CodePudding user response:

Fir of all move for loop from inside enter_data of personal data to inside choice 1 so that it start creating personal data objects based in entered size

That way it will append your unique personal Data objects o the data list

Here is the correct code after changes

data = []

class PersonsData(object):
    # Object constructor
    def __init__(self):
        print("Person Data")
        self.name = ''
        self.age = 0
        self.doB = 0

    # Input Method
    def enter_data(self):

        self.name = str(input("Enter Your Name"   " ").upper())

        try:
            self.age = int(input("Enter Your Age"   " "))
        except:
            print("\n**Enter value in Number**")
            self.age = int(input("Enter Your Age"   " "))

        self.doB = (input("Enter Your DOB"   " "))
        print("\n")

    # Display Method
    def display(self):
        print("Name:", self.name)
        print("Age:", self.age)
        print("DOB:", self.doB)


while True:
    print()
    print("""
    1.Add New Detail
    2.Display Detail
    3.Quit
    """)
    choice = int(input("Enter Choice:"   " "))
    if choice == 1:
        size = int(input("Enter the number of data"))
        for i in range(size):
            info = PersonsData()
            info.enter_data()
            print(info.name)
            data.append(info)

        print(data)

    elif choice == 2:
        for i in data:
            print("--------------------------------")
            i.display()
            print("--------------------------------")

    elif choice == 3:
        quit()

    else:
        print("Invalid choice")

  • Related