Sorting student info Problem

Thread Solved
Reply

Join Date: Oct 2006
Posts: 34
Reputation: babutche is an unknown quantity at this point 
Solved Threads: 0
babutche babutche is offline Offline
Light Poster

Sorting student info Problem

 
0
  #1
Dec 26th, 2006
Hi Everyone!

I need a little help with this problem. I am trying to import a class -- Student with object makestudent but I keep getting this error:





  1. This program sorts student grade information
  2. Enter the name of the data file: p2sortin.py
  3. Traceback (most recent call last):
  4. File "C:\Python23\p2sort.py", line 37, in ?
  5. main()
  6. File "C:\Python23\p2sort.py", line 31, in main
  7. data = readStudents(filename)
  8. File "C:\Python23\p2sort.py", line 11, in readStudents
  9. for line in infile:
  10. ValueError: I/O operation on closed file
  11. >>>
Can anyone help me with what i could possibly be doing wrong?

This is my program that I am testing from the book:
This is only the beginning of my problem and i can't even get it to work!!



  1. from gpa import Student, makeStudent
  2. def readStudents(filename):
  3. infile = open(filename, 'r')
  4. students = []
  5. for line in infile:
  6. students.append(makeStudent(line))
  7. infile.close()
  8. return students
  9.  
  10. def writeStudents(students, filename):
  11. outfile = open(filename, 'w')
  12. for s in students:
  13. outfile.write("%s\t%f\t%f\n" %
  14. (s.getName(), s.getHours(), s.getQPoints()))
  15. outfile.close()
  16.  
  17. def cmpGPA(s1, s2):
  18. return cmp(s1.gpa(), s2.gpa())
  19. def main():
  20. print "This program sorts student grade information"
  21. filename = raw_input("Enter the name of the data file: ")
  22. data = readStudents(filename)
  23. data.sort(cmpGPA)
  24. filename = raw_input("Enter a name for the output file: ")
  25. writeStudents(data, filename)
  26. print "The data has been written to", filename
  27. if __name__ == '__main__':
  28. main()

This is the file that I am trying to import:


  1. import string
  2. import math
  3. class Student:
  4. def __init__(self, name, hours, qpoints):
  5. self.name = name
  6. self.hours = float(hours)
  7. self.qpoints = float(qpoints)
  8. def getName(self):
  9. return self.name
  10. def getHours(self):
  11. return self.hours
  12. def getQPoints(self):
  13. return self.qpoints
  14. def gpa(self):
  15. return self.qpoints/self.hours
  16. def makeStudent(infoStr):
  17. name, hours, qpoints = string.split(infoStr,"\t")
  18. return Student(name, hours, qpoints)
  19. def main():
  20. filename = raw_input("Enter name the grade file: ")
  21. infile = open(filename, 'r')
  22. best = makeStudent(infile.readline())
  23. for line in infile:
  24. s = makeStudent(line)
  25. if s.gpa() > best.gpa():
  26. best = s
  27. infile.close()
  28. print "The best student is:", best.getName()
  29. print "hours:", best.getHours()
  30. print "GPA:", best.gpa()
  31. if __name__ == '__main__':
  32. main()


And this is my input file name and data: p2sortin.py


  1. Adams, Henry 127 228
  2. Computewell, Susan 100 400
  3. DibbleBit, Denny 18 41.5
  4. Jones, Jim 48.5 155
  5. Smith, Frank 37 125.33



It seems to me that it is telling me the file is closed but i can' figure it out!

Thanks!
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 34
Reputation: babutche is an unknown quantity at this point 
Solved Threads: 0
babutche babutche is offline Offline
Light Poster

Re: Sorting student info Problem

 
0
  #2
Dec 26th, 2006
The infile and the outfile were indented into the loop. I emailed my instructor and he figured out what was going wrong. Thanks anyway.

Problem solved so far.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 1,514
Reputation: Ene Uran has a spectacular aura about Ene Uran has a spectacular aura about 
Solved Threads: 168
Ene Uran's Avatar
Ene Uran Ene Uran is offline Offline
Posting Virtuoso

Re: Sorting student info Problem

 
0
  #3
Dec 27th, 2006
Always check you statement blocks! After you program with Python for a short while, these kind of things stick out like a sore thumb.
drink her pretty
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Python Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC