I have a text file that looks something like this where the first column is a student's name, the second column is the number of credits, and the third is the number of points (grade times hours).
john 5 15
bill 9 30
ted 7 22
I want to create a class that extracts the relevant information and calculates gpa.
class Student:
def __init__(self, name, hours, qpoints):
self.name = name
self.hours = float(hours)
self.qpoints = float(qpoints)
def getName(self):
return self.name
def getHours(self):
return self.hours
def getQPoints(self):
return self.qpoints
def gps(self):
return self.qpoints/self.hours
used to make extract the data (based on the fact that there is a tab between each piece of information)
def makeStudent(info):
name, hours, qpoints = info.split("\t")
return Student(name, hours, qpoints)
here I use a for loop to create a list based on the data in the text file by appending the relevant information from each line to the list
def readStudents(filename):
infile = open(filename, 'r')
students = []
for line in infile:
students.append(makeStudent(line))
infile.close()
return students
the problem is that I get this error:
[<__main__.Student object at 0x01FA4AD0>, <__main__.Student object at 0x01FA4AF0>,
<__main__.Student object at 0x01FA4B10>, <__main__.Student object at 0x01FA4B50>,
<__main__.Student object at 0x01FA4B30>]
Any ideas on why this is happening?
printing all your student objects. I think you wanted toprintStudent.name,Student.hours, etc. If you just sayprint Student, this is what will happen.return Student(name, hours, qpoints)returns an instance of a class.[<__main__.Student object at 0x01FA4AD0>describes an instance of a class.students.append(Student(*line.split("\t")))