Classes: How to Print Specific Attributes - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Classes: How to Print Specific Attributes (/thread-13408.html) |
Classes: How to Print Specific Attributes - emerger - Oct-13-2018 Hello. I'm having trouble printing specific attributes from my object-array. My code is below, and it contains my class Student, class Array, along with two function (main, and the function that creates the array. I'm trying to print the first and last names, contained within the array, from my main() under the second 'if' statement. My menu in main() shows our tasks to accomplish. Note: Please forgive any naming convention violations as they are meant to be descriptive and will be cleaned up later when my class partner and I come together later. This is a joint project with another student, so some of the names are not of my creation. Here is my main function: from classStudent import Student from classArray import Array from linkStudentsToClass import loadCSVdatas ''' Main function contians 'if' statements to execute methods from user input''' def main(): continue_on = 'y' while continue_on == 'y': userInput = menu() if userInput == 1: newArray = loadCSVdatas() print("\nHere is your student database:\n") print(newArray) for line in newArray: print(line) continue_on = input("Would you like to return to the main menu? y or n: ") if continue_on == 'y': continue else: break if userInput == 2: newArray = loadCSVdatas() studentData = Student(Id, First_Name, Last_Name, Programe, Start_Date, Adv_Last_Name, Most_Recent, GPA) for i in newArray: print (Student(newArray(First_Name))) continue_on = input("Would you like to return to the main menu? y or n: ") if continue_on == 'y': continue else: break ''' Menu function''' def menu(): print("Welcome to Singh's and Pete's Student database.\n" "Please choose from the following menu options:\n" "1. Load and View the Student Database\n" "2. List all students\n" "3. List all students with their programs Program\n" "4. Exit") userInput = int(input()) return userInput if __name__ == "__main__": main()Here is my module (linkStudentsToClass) that creates my array. from classStudent import Student from classArray import Array import csv def loadCSVdatas(): with open("student_advisees_testdata.csv",'r') as csv_file: reader = csv.reader(csv_file) next(reader) index = 0 newArray = Array(10) for line in reader: studentAttributes = Student(line[0],line[1],line[2],line[3],line[4],line[5],line[6],line[7]) newArray[index] = studentAttributes index += 1 return newArrayHere is my Student class (classStudent): class Student(object): def __init__(self, Id, First_Name, Last_Name, Programe, Start_Date, Adv_Last_Name, Most_Recent, GPA): self.Id = Id self.First_Name = First_Name self.Last_Name = Last_Name self.Programe = Programe self.Start_Date = Start_Date self.Adv_Last_Name = Adv_Last_Name self.Most_Recent = Most_Recent self.GPA = GPA def __str__(self): return str(self.Id) +" " + str(self.First_Name) + " " + str(self.Last_Name) + " " + str(self.Programe) + " " + str(self.Start_Date) + " " + str(self.Adv_Last_Name) + " " + str(self.Most_Recent) + " " + str(self.GPA)And here is my class Array(classArray) class Array(object): """Represents an array.""" def __init__(self, capacity, fillValue = None): """Capacity is the static size of the array. fillValue is placed at each position.""" self._items = list() for count in range(capacity): self._items.append(fillValue) def __len__(self): """-> The capacity of the array.""" return len(self._items) def __str__(self): """-> The string representation of the array.""" return str(self._items) def __iter__(self): """Supports iteration over a view of an array.""" return iter(self._items) def __getitem__(self, index): """Subscript operator for access at index.""" return self._items[index] def __setitem__(self, index, newItem): """Subscript operator for replacement at index.""" self._items[index] = newItem RE: Classes: How to Print Specific Attributes - emerger - Oct-13-2018 It seems to me that if I were to create the following method() in the classStudent, I now have a method within the class to print full name. However I'm not sure how to get the First_Name and Last_Name from the array, in my main, to print from this. I don't know how these are all linked! def fullName(self): self.full_name = self.First_Name + ' ' + self.Last_Name RE: Classes: How to Print Specific Attributes - marienbad - Oct-13-2018 for student in newArray: print student.firstname + " " + student.lastnameon lines 27 and 28. Does this work? RE: Classes: How to Print Specific Attributes - emerger - Oct-13-2018 Yes! Thank you so much. It is so simple! I think I tried everything else. Truly, I am grateful. |