0

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")
3
  • There shouldn't be a loop in enter_data. It's only setting up that one object Commented Jan 3, 2022 at 9:52
  • in enter_data(self) method you should return something like return self Commented Jan 3, 2022 at 9:52
  • so, where should I use the loop then? Commented Jan 3, 2022 at 9:55

1 Answer 1

1

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")

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.