<!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> File "D:\Python22\grades.py", line 99, in
?<BR> save_grades(students,filename)<BR> File
"D:\Python22\grades.py", line 51, in save_grades<BR>
out_file.write(x+","+max_points[x]+"\n")<BR>TypeError: sequence index must be
integer</DIV>
<DIV> </DIV>
<DIV>And the code:</DIV>
<DIV>max_points = [25,25,50,25,100]<BR>assignments = ['hw ch 1','hw ch
2','quiz ','hw ch 3','test']<BR>students = {'#Max':max_points}</DIV>
<DIV> </DIV>
<DIV>def print_menu():<BR> print "1. Add
student"<BR> print "2. Remove student"<BR>
print "3. Print grades"<BR> print "4. Record
grade"<BR> print "5. Load Grades"<BR> print
"6. Save Grades"<BR> print "9. Exit"</DIV>
<DIV> </DIV>
<DIV>def print_all_grades():<BR> print
'\t',<BR> for i in
range(len(assignments)):<BR> print
assignments[1],'\t',<BR> print<BR> keys =
students.keys()<BR> keys.sort()<BR> for x in
keys:<BR> print
x,'\t',<BR> grades =
students[x]<BR>
print_grades(grades)</DIV>
<DIV> </DIV>
<DIV>def print_grades(grades):<BR> for i in
range(len(grades)):<BR> print
grades[i],'\t\t',<BR> print</DIV>
<DIV> </DIV>
<DIV>def choice():<BR> return int(raw_input("Menu Choice:
"))</DIV>
<DIV> </DIV>
<DIV>def school():<BR> return raw_input("Student: ")</DIV>
<DIV> </DIV>
<DIV>def load_grades(students,filename):<BR> in_file =
open(filename, "r")<BR> while
1:<BR> in_line =
in_file.readline()<BR> if in_line ==
"":<BR>
break<BR> in_line =
in_line[:-1]<BR> [students,max_points]
= string.split(in_line,",")<BR>
max_points[students] = grade<BR> in_file.close()</DIV>
<DIV> </DIV>
<DIV>def save_grades(students,filename):<BR> out_file =
open(filename, "w")<BR> for x in
students.keys():<BR>
out_file.write(x+","+max_points[x]+"\n")<BR>
out_file.close</DIV>
<DIV> </DIV>
<DIV>print "Grade Tracking Program."<BR>while 1:<BR>
print_menu()<BR> menu_choice = choice()<BR>
if menu_choice == 1:<BR> print "Add
student"<BR> name =
school()<BR> students[name] =
[0]*len(max_points)<BR> elif menu_choice ==
2:<BR> print "Remove
student"<BR> name =
school()<BR> if
students.has_key(name):<BR>
del students[name]<BR>
else:<BR>
print "Student: ",name," not found."<BR> elif menu_choice ==
3:<BR> print_all_grades()</DIV>
<DIV> </DIV>
<DIV> elif menu_choice ==
4:<BR> print "Record
Grade"<BR> name =
school()<BR> if
students.has_key(name):<BR>
grades =
students[name]<BR>
print "Type in the number of the grade to
record"<BR>
print "Type in a 0 (zero) to
exit"<BR> for
i in
range(len(assignments)):<BR>
print i+1,'
',assignments[i],'\t',<BR>
print<BR>
print_grades(grades)<BR>
which =
1234<BR> while
which !=
-1:<BR>
which = int(raw_input("Change which Grade:
"))<BR>
which =
which-1<BR>
if 0 <= which <
len(grades):<BR>
grade = int(raw_input("Grade:
"))<BR>
grades[which] =
grade<BR>
elif which !=
-1:<BR>
print "Invalid Grade
Number"<BR>
else:<BR>
print "Student not found"<BR> elif menu_choice ==
5:<BR> filename = raw_input("Filename
to load: ")<BR>
load_grades(students,filename)<BR> elif menu_choice ==
6:<BR> filename = raw_input("Filename
to save: ")<BR>
save_grades(students,filename)<BR> elif menu_choice ==
9:<BR> break<BR>
else:<BR> print "That's not a
choice!"<BR>print "Goodbye."<BR></DIV>
<DIV>What's the problem, and how is it fixed?</DIV>
<DIV> </DIV>
<DIV>Thanks,</DIV>
<DIV>Nathan Pinno,<BR>Crew, Camrose McDonalds and owner/operator of
Woffee</DIV></BODY></HTML>