<!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>&gt;&gt;&gt; max_points = [25,25,50,25,100]<BR>&gt;&gt;&gt; 
  assignments = ['hw ch 1','hw ch 2','quiz&nbsp;&nbsp; ','hw ch 
  3','test']<BR>&gt;&gt;&gt; students = {'#Max':max_points}<BR>&gt;&gt;&gt; 
  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> &lt;<A href="mailto:falcon3166@hotmail.com ">falcon3166@hotmail.com 
  </A>&gt; 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>&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><SPAN class=sg>
    <DIV>Nathan Pinno,<BR>Crew, Camrose McDonalds and owner/operator of 
    Woffee</DIV></SPAN><BR>_______________________________________________<BR>Tutor 
    maillist &nbsp;- &nbsp;<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>