<!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>Maybe I should use assignments instead?</DIV>
<BLOCKQUOTE
style="PADDING-RIGHT: 0px; PADDING-LEFT: 5px; MARGIN-LEFT: 5px; BORDER-LEFT: #000000 2px solid; MARGIN-RIGHT: 0px">
<DIV style="FONT: 10pt arial">----- Original Message ----- </DIV>
<DIV
style="BACKGROUND: #e4e4e4; FONT: 10pt arial; font-color: black"><B>From:</B>
<A title=adam.jtm30@gmail.com href="mailto:adam.jtm30@gmail.com">Adam Bark</A>
</DIV>
<DIV style="FONT: 10pt arial"><B>To:</B> <A title=falcon3166@hotmail.com
href="mailto:falcon3166@hotmail.com">Nathan Pinno</A> </DIV>
<DIV style="FONT: 10pt arial"><B>Cc:</B> <A title=tutor@python.org
href="mailto:tutor@python.org">tutor@python.org</A> </DIV>
<DIV style="FONT: 10pt arial"><B>Sent:</B> Monday, August 01, 2005 2:47
PM</DIV>
<DIV style="FONT: 10pt arial"><B>Subject:</B> Re: [Tutor] I've run into a jam
on the exercise on file I/O</DIV>
<DIV><BR></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}<BR>>>>
students<BR>{'#Max': [25, 25, 50, 25, 100]}<BR><BR>The problem is the key in
students is a list not an integer.<BR><BR>
<DIV><SPAN class=gmail_quote>On 8/1/05, <B class=gmail_sendername>Nathan
Pinno</B> <<A href="mailto:falcon3166@hotmail.com ">falcon3166@hotmail.com
</A>> wrote:</SPAN>
<BLOCKQUOTE class=gmail_quote
style="PADDING-LEFT: 1ex; MARGIN: 0pt 0pt 0pt 0.8ex; BORDER-LEFT: rgb(204,204,204) 1px solid">
<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><SPAN class=sg>
<DIV>Nathan Pinno,<BR>Crew, Camrose McDonalds and owner/operator of
Woffee</DIV></SPAN><BR>_______________________________________________<BR>Tutor
maillist - <A
onclick="return top.js.OpenExtLink(window,event,this)"
href="mailto:Tutor@python.org">Tutor@python.org</A><BR><A
onclick="return top.js.OpenExtLink(window,event,this)"
href="http://mail.python.org/mailman/listinfo/tutor"
target=_blank>http://mail.python.org/mailman/listinfo/tutor</A><BR><BR><BR><BR
clear=all></BLOCKQUOTE></DIV><BR></BLOCKQUOTE></BODY></HTML>