I am trying to extend my "Student with the best GPA" program so that it allows the user to sort a file of students based on gpa, name, or credits. The program needs to prompt for the input and output files and also the field to sort on (gpa, name or credits).
The input file consists of students name, credits(hours) and quality points (gpa = quality points / hours)
I keep getting an error that global name "gpa" is not defined and I don't understand why. Can anyone steer me in the right direction as to where I am going wrong? Thank you!
This is what I have so far on my program:
from p2gpa 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 cmpname(s1, s2): return cmp("s1.name()", "s2.name()") def cmpcredits(s1, s2): return cmp(s1.credits(), s2.credits()) def main(): print "This program sorts student grade information" filename = raw_input("Enter the name of the data file: ") dfield = raw_input("Enter gpa, name, or credits to sort: ") filename = raw_input("Enter a name for the output file: ") data = readStudents(filename) if dfield == gpa: data.sort(cmpgpa) elif dfield == name: data.sort(cmpname) else: data.sort(cmpcredits) 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 p2gpa program that I imported:
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 name(self): return self.name 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()
This is the input file I want to sort (p2sortin.py):
Computewell, Susan 100 400 DibbleBit, Denny 18 41.5 Jones, Jim 48.5 155 Smith, Frank 37 125.33 Adams, Henry 127 228
This is the error that I keep getting:
This program sorts student grade information Enter the name of the data file: p2sortin.py Enter gpa, name, or credits to sort: gpa Enter a name for the output file: p2sortout.py Traceback (most recent call last): File "C:\Python23\p2sort.py", line 50, in ? main() File "C:\Python23\p2sort.py", line 40, in main if dfield == gpa: NameError: global name 'gpa' is not defined >>>