| | |
Sorting student info Problem
Thread Solved
![]() |
•
•
Join Date: Oct 2006
Posts: 34
Reputation:
Solved Threads: 0
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:
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!!
This is the file that I am trying to import:
And this is my input file name and data: p2sortin.py
It seems to me that it is telling me the file is closed but i can' figure it out!
Thanks!
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:
Python Syntax (Toggle Plain Text)
This program sorts student grade information Enter the name of the data file: p2sortin.py Traceback (most recent call last): File "C:\Python23\p2sort.py", line 37, in ? main() File "C:\Python23\p2sort.py", line 31, in main data = readStudents(filename) File "C:\Python23\p2sort.py", line 11, in readStudents for line in infile: ValueError: I/O operation on closed file >>>
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!!
Python Syntax (Toggle Plain Text)
from gpa import Student, makeStudent def readStudents(filename): infile = open(filename, 'r') students = [] for line in infile: students.append(makeStudent(line)) infile.close() return students def writeStudents(students, filename): outfile = open(filename, 'w') for s in students: outfile.write("%s\t%f\t%f\n" % (s.getName(), s.getHours(), s.getQPoints())) outfile.close() def cmpGPA(s1, s2): return cmp(s1.gpa(), s2.gpa()) def main(): print "This program sorts student grade information" filename = raw_input("Enter the name of the data file: ") data = readStudents(filename) data.sort(cmpGPA) filename = raw_input("Enter a name for the output file: ") writeStudents(data, filename) print "The data has been written to", filename if __name__ == '__main__': main()
This is the file that I am trying to import:
Python Syntax (Toggle Plain Text)
import string import math 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 gpa(self): return self.qpoints/self.hours def makeStudent(infoStr): name, hours, qpoints = string.split(infoStr,"\t") return Student(name, hours, qpoints) def main(): filename = raw_input("Enter name the grade file: ") infile = open(filename, 'r') best = makeStudent(infile.readline()) for line in infile: s = makeStudent(line) if s.gpa() > best.gpa(): best = s infile.close() print "The best student is:", best.getName() print "hours:", best.getHours() print "GPA:", best.gpa() if __name__ == '__main__': main()
And this is my input file name and data: p2sortin.py
Python Syntax (Toggle Plain Text)
Adams, Henry 127 228 Computewell, Susan 100 400 DibbleBit, Denny 18 41.5 Jones, Jim 48.5 155 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!
![]() |
Similar Threads
- Problem while installing linux (*nix Software)
- C++ help with student array problem (C++)
- Hotoffers.info (Viruses, Spyware and other Nasties)
- 2038 Problem (C++)
- SAMBA --> XP problem (*nix Software)
- problem with accessing sites (Web Browsers)
- Motherboard Nuked...Need to back up info on HD. (Storage)
Other Threads in the Python Forum
- Previous Thread: Ext Class
- Next Thread: Python Executable (Py2Exe)
| Thread Tools | Search this Thread |
alarm ansi anydbm app assignment backend beginner binary bluetooth character cipher cmd coordinates customdialog cx-freeze data decimals development directory dynamic exe feet file float format function generator getvalue gnu graphics halp handling heads homework http ideas input ip itunes java keycontrol leftmouse line linux list lists loop maintain maze millimeter module mouse number numbers output parsing path pointer prime programming progressbar push py2exe pygame pymailer python queue random recursion recursive schedule screensaverloopinactive script slicenotation sqlite ssh statistics string strings sudokusolver text thread time tlapse tuple ubuntu unicode url urllib urllib2 variable ventrilo vigenere web webservice wikipedia write wxpython xlib xlwt






