Python Forum
Classes: How to Print Specific Attributes
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Classes: How to Print Specific Attributes
#1
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 newArray
Here 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     
Reply
#2
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
Reply
#3
for student in newArray:
	print student.firstname + " " + student.lastname
on lines 27 and 28.

Does this work?
Reply
#4
Yes! Thank you so much. It is so simple! I think I tried everything else. Truly, I am grateful.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question [solved] Classes, assign an attributes to a class not to instances.. SpongeB0B 4 891 May-20-2023, 04:08 PM
Last Post: SpongeB0B
  python print all files which contain specific word in it mg24 5 1,188 Jan-27-2023, 11:20 AM
Last Post: snippsat
  Listing Attributes of Objects & Classes JoeDainton123 4 2,317 Aug-28-2020, 05:27 AM
Last Post: ndc85430
  Getting attributes from a list containing classes midarq 2 2,112 Dec-03-2019, 12:42 AM
Last Post: midarq
  Delete specific lines contain specific words mannyi 2 4,069 Nov-04-2019, 04:50 PM
Last Post: mannyi
  print a word after specific word search evilcode1 8 4,721 Oct-22-2019, 08:08 AM
Last Post: newbieAuggie2019
  Using classes? Can I just use classes to structure code? muteboy 5 4,978 Nov-01-2017, 04:20 PM
Last Post: metulburr

Forum Jump:

User Panel Messages

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