Find answers, ask questions, and connect with our <br>community around the world.

Home Forums Scientific computing with Python Lesson 22 Objects as attributes

  • Lesson 22 Objects as attributes

    Posted by Mathanraj on March 8, 2024 at 3:01 am

    Hi!

    While trying to complete assignment in Lesson 22, I’m facing an issue that I don’t know how to store all the student’s details in one list.

    I will attach the screenshot of the program. Kindly check & let me know what is the issue.

    Thank you in advance!

    Have a nice day!

    Mathanraj replied 1 week, 4 days ago 2 Members · 3 Replies
  • 3 Replies
  • Nishant

    Member
    April 9, 2024 at 12:02 pm

    Hi Mathanraj,

    Apologies for the long delay in replying to your query. I missed a few questions in the last few weeks. I will suggest the following approach.
    1. Make a method in the Student class that returns the details of a student instance. This can be done by,
    def __str__(self):
    return f”{self.name}, {self.age}, {self.major}”

    2. The __init__ method of the University class will contain the list attribute to store student objects
    def __init__(self):
    self.students = []
    Add a method to add student instances to the list,
    def add_student(self, student):
    self.students.append(student)
    Add a method to display the list contents,
    def display_students(self):
    for student in self.students:
    print(student)

    Hope that helps.

  • Nishant

    Member
    April 9, 2024 at 12:04 pm

    A screenshot of the working code is attached herewith for your reference.

  • Mathanraj

    Member
    April 9, 2024 at 4:29 pm

    Thank you Nishanth for ur reply!

    Already submitted my assignment where I used a method to get student details & store automatically. Please check it!

    Thank you!

Log in to reply.

error: Content is protected !!