![]() |
| ||
| Sorting a File Hi, 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 This is the p2gpa program that I imported: import string This is the input file I want to sort (p2sortin.py): Computewell, Susan 100 400 This is the error that I keep getting: This program sorts student grade information Thanks! |
| ||
| Re: Sorting a File Your initial problem is right here: def main():You are assigning the same variable 'filename' to both the input and output file. Your next problem is here: if dfield == gpa:The input function raw_input() returns a string, so this ought to be: if dfield == 'gpa':Give your data file a name like in.dat, don't use .py (it is not Python code!). Your next problem is here ( I added a test print): def makeStudent(infoStr):The result of splitting a string is a list, not a tuple! Also the data file needs tabs at the right places to make this work! |
| ||
| Re: Sorting a File Quote:
if dfield == "gpa": A simple menu would be easier for the user, with 1=gpa, etc. In any case, you should have a list of correct responses, or be able to add something like 0 < number < 4, to check for a valid input. |
| ||
| Re: Sorting a File I played with the code for a while and made these observations and corrections ... # save as p2sort.pyHere is the module file ... # save this module file as p2gpa.pyI also would recommend to use extension .dat or .txt for your data files, and make sure the text editor you use leaves the tab separator intact. Some editors replace tabs with spaces! This little code will produce a correct sample data file called 'in.txt' ... # this will create a sample data file with tab separators |
| ||
| Re: Sorting a File Thank you all very much for your input!! I will have to study this and see if I can figure out exactly what is going on (so I know what I am doing). Now I see that one of my major problems was that I was trying to return: return cmp(s1.name, etc..) instead of return cmp(s1.getName, etc...) (same thing for Hours) I have also fixed the other problem where I did not have my quotation marks in the proper places. Now I just have to fix a few more things in my files and I should be able to finish!! Thank You For Helping!! |
| All times are GMT -4. The time now is 1:12 pm. |
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC