<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=Content-Type content="text/html; charset=iso-8859-1">
<META content="MSHTML 6.00.2900.2668" name=GENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=#ffffff>
<DIV>Hey all,</DIV>
<DIV>I've seem to run into a jam while working on the exercise on file 
I/O.</DIV>
<DIV>Here's the error:</DIV>
<DIV>Filename to save: university.txt<BR>Traceback (most recent call 
last):<BR>&nbsp; File "D:\Python22\grades.py", line 99, in 
?<BR>&nbsp;&nbsp;&nbsp; save_grades(students,filename)<BR>&nbsp; File 
"D:\Python22\grades.py", line 51, in save_grades<BR>&nbsp;&nbsp;&nbsp; 
out_file.write(x+","+max_points[x]+"\n")<BR>TypeError: sequence index must be 
integer</DIV>
<DIV>&nbsp;</DIV>
<DIV>And the code:</DIV>
<DIV>max_points = [25,25,50,25,100]<BR>assignments = ['hw ch 1','hw ch 
2','quiz&nbsp;&nbsp; ','hw ch 3','test']<BR>students = {'#Max':max_points}</DIV>
<DIV>&nbsp;</DIV>
<DIV>def print_menu():<BR>&nbsp;&nbsp;&nbsp; print "1. Add 
student"<BR>&nbsp;&nbsp;&nbsp; print "2. Remove student"<BR>&nbsp;&nbsp;&nbsp; 
print "3. Print grades"<BR>&nbsp;&nbsp;&nbsp; print "4. Record 
grade"<BR>&nbsp;&nbsp;&nbsp; print "5. Load Grades"<BR>&nbsp;&nbsp;&nbsp; print 
"6. Save Grades"<BR>&nbsp;&nbsp;&nbsp; print "9. Exit"</DIV>
<DIV>&nbsp;</DIV>
<DIV>def print_all_grades():<BR>&nbsp;&nbsp;&nbsp; print 
'\t',<BR>&nbsp;&nbsp;&nbsp; for i in 
range(len(assignments)):<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print 
assignments[1],'\t',<BR>&nbsp;&nbsp;&nbsp; print<BR>&nbsp;&nbsp;&nbsp; keys = 
students.keys()<BR>&nbsp;&nbsp;&nbsp; keys.sort()<BR>&nbsp;&nbsp;&nbsp; for x in 
keys:<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print 
x,'\t',<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; grades = 
students[x]<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
print_grades(grades)</DIV>
<DIV>&nbsp;</DIV>
<DIV>def print_grades(grades):<BR>&nbsp;&nbsp;&nbsp; for i in 
range(len(grades)):<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print 
grades[i],'\t\t',<BR>&nbsp;&nbsp;&nbsp; print</DIV>
<DIV>&nbsp;</DIV>
<DIV>def choice():<BR>&nbsp;&nbsp;&nbsp; return int(raw_input("Menu Choice: 
"))</DIV>
<DIV>&nbsp;</DIV>
<DIV>def school():<BR>&nbsp;&nbsp;&nbsp; return raw_input("Student: ")</DIV>
<DIV>&nbsp;</DIV>
<DIV>def load_grades(students,filename):<BR>&nbsp;&nbsp;&nbsp; in_file = 
open(filename, "r")<BR>&nbsp;&nbsp;&nbsp; while 
1:<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; in_line = 
in_file.readline()<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if in_line == 
"":<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
break<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; in_line = 
in_line[:-1]<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [students,max_points] 
= string.split(in_line,",")<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
max_points[students] = grade<BR>&nbsp;&nbsp;&nbsp; in_file.close()</DIV>
<DIV>&nbsp;</DIV>
<DIV>def save_grades(students,filename):<BR>&nbsp;&nbsp;&nbsp; out_file = 
open(filename, "w")<BR>&nbsp;&nbsp;&nbsp; for x in 
students.keys():<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
out_file.write(x+","+max_points[x]+"\n")<BR>&nbsp;&nbsp;&nbsp; 
out_file.close</DIV>
<DIV>&nbsp;</DIV>
<DIV>print "Grade Tracking Program."<BR>while 1:<BR>&nbsp;&nbsp;&nbsp; 
print_menu()<BR>&nbsp;&nbsp;&nbsp; menu_choice = choice()<BR>&nbsp;&nbsp;&nbsp; 
if menu_choice == 1:<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print "Add 
student"<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; name = 
school()<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; students[name] = 
[0]*len(max_points)<BR>&nbsp;&nbsp;&nbsp; elif menu_choice == 
2:<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print "Remove 
student"<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; name = 
school()<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if 
students.has_key(name):<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
del students[name]<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
else:<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
print "Student: ",name," not found."<BR>&nbsp;&nbsp;&nbsp; elif menu_choice == 
3:<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print_all_grades()</DIV>
<DIV>&nbsp;</DIV>
<DIV>&nbsp;&nbsp;&nbsp; elif menu_choice == 
4:<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print "Record 
Grade"<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; name = 
school()<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if 
students.has_key(name):<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
grades = 
students[name]<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
print "Type in the number of the grade to 
record"<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
print "Type in a 0 (zero) to 
exit"<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for 
i in 
range(len(assignments)):<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
print i+1,' 
',assignments[i],'\t',<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
print<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
print_grades(grades)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
which = 
1234<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; while 
which != 
-1:<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
which = int(raw_input("Change which Grade: 
"))<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
which = 
which-1<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
if 0 &lt;= which &lt; 
len(grades):<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
grade = int(raw_input("Grade: 
"))<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
grades[which] = 
grade<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
elif which != 
-1:<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
print "Invalid Grade 
Number"<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
else:<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
print "Student not found"<BR>&nbsp;&nbsp;&nbsp; elif menu_choice == 
5:<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; filename = raw_input("Filename 
to load: ")<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
load_grades(students,filename)<BR>&nbsp;&nbsp;&nbsp; elif menu_choice == 
6:<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; filename = raw_input("Filename 
to save: ")<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
save_grades(students,filename)<BR>&nbsp;&nbsp;&nbsp; elif menu_choice == 
9:<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; break<BR>&nbsp;&nbsp;&nbsp; 
else:<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print "That's not a 
choice!"<BR>print "Goodbye."<BR></DIV>
<DIV>What's the problem, and how is it fixed?</DIV>
<DIV>&nbsp;</DIV>
<DIV>Thanks,</DIV>
<DIV>Nathan Pinno,<BR>Crew, Camrose McDonalds and owner/operator of 
Woffee</DIV></BODY></HTML>