[Tutor] I've run into a jam on the exercise on file I/O

Adam Bark adam.jtm30 at gmail.com
Mon Aug 1 22:55:17 CEST 2005


No sorry scratch that last one I was being stupid. The key is a string but 
max_points is a list so the list index max_points[x] is effectively looking 
for max_points[student].

def save_grades(students,filename):
out_file = open(filename, "w")
for x in students.keys():
out_file.write(x+","+max_points[x]+"\n")
out_file.close

On 8/1/05, Adam Bark <adam.jtm30 at gmail.com> wrote:
> 
> >>> max_points = [25,25,50,25,100]
> >>> assignments = ['hw ch 1','hw ch 2','quiz ','hw ch 3','test']
> >>> students = {'#Max':max_points}
> >>> students
> {'#Max': [25, 25, 50, 25, 100]}
> 
> The problem is the key in students is a list not an integer.
> 
> On 8/1/05, Nathan Pinno <falcon3166 at hotmail.com > wrote:
> 
> > Hey all,
> > I've seem to run into a jam while working on the exercise on file I/O.
> > Here's the error:
> > Filename to save: university.txt
> > Traceback (most recent call last):
> > File "D:\Python22\grades.py", line 99, in ?
> > save_grades(students,filename)
> > File "D:\Python22\grades.py", line 51, in save_grades
> > out_file.write(x+","+max_points[x]+"\n")
> > TypeError: sequence index must be integer
> >  And the code:
> > max_points = [25,25,50,25,100]
> > assignments = ['hw ch 1','hw ch 2','quiz ','hw ch 3','test']
> > students = {'#Max':max_points}
> >  def print_menu():
> > print "1. Add student"
> > print "2. Remove student"
> > print "3. Print grades"
> > print "4. Record grade"
> > print "5. Load Grades"
> > print "6. Save Grades"
> > print "9. Exit"
> >  def print_all_grades():
> > print '\t',
> > for i in range(len(assignments)):
> > print assignments[1],'\t',
> > print
> > keys = students.keys()
> > keys.sort()
> > for x in keys:
> > print x,'\t',
> > grades = students[x]
> > print_grades(grades)
> >  def print_grades(grades):
> > for i in range(len(grades)):
> > print grades[i],'\t\t',
> > print
> >  def choice():
> > return int(raw_input("Menu Choice: "))
> >  def school():
> > return raw_input("Student: ")
> >  def load_grades(students,filename):
> > in_file = open(filename, "r")
> > while 1:
> > in_line = in_file.readline()
> > if in_line == "":
> > break
> > in_line = in_line[:-1]
> > [students,max_points] = string.split(in_line,",")
> > max_points[students] = grade
> > in_file.close()
> >  def save_grades(students,filename):
> > out_file = open(filename, "w")
> > for x in students.keys():
> > out_file.write(x+","+max_points[x]+"\n")
> > out_file.close
> >  print "Grade Tracking Program."
> > while 1:
> > print_menu()
> > menu_choice = choice()
> > if menu_choice == 1:
> > print "Add student"
> > name = school()
> > students[name] = [0]*len(max_points)
> > elif menu_choice == 2:
> > print "Remove student"
> > name = school()
> > if students.has_key(name):
> > del students[name]
> > else:
> > print "Student: ",name," not found."
> > elif menu_choice == 3:
> > print_all_grades()
> >   elif menu_choice == 4:
> > print "Record Grade"
> > name = school()
> > if students.has_key(name):
> > grades = students[name]
> > print "Type in the number of the grade to record"
> > print "Type in a 0 (zero) to exit"
> > for i in range(len(assignments)):
> > print i+1,' ',assignments[i],'\t',
> > print
> > print_grades(grades)
> > which = 1234
> > while which != -1:
> > which = int(raw_input("Change which Grade: "))
> > which = which-1
> > if 0 <= which < len(grades):
> > grade = int(raw_input("Grade: "))
> > grades[which] = grade
> > elif which != -1:
> > print "Invalid Grade Number"
> > else:
> > print "Student not found"
> > elif menu_choice == 5:
> > filename = raw_input("Filename to load: ")
> > load_grades(students,filename)
> > elif menu_choice == 6:
> > filename = raw_input("Filename to save: ")
> > save_grades(students,filename)
> > elif menu_choice == 9:
> > break
> > else:
> > print "That's not a choice!"
> > print "Goodbye."
> > What's the problem, and how is it fixed?
> >  Thanks,
> > Nathan Pinno,
> > Crew, Camrose McDonalds and owner/operator of Woffee
> > 
> > _______________________________________________
> > Tutor maillist - Tutor at python.org
> > http://mail.python.org/mailman/listinfo/tutor
> > 
> > 
> > 
> > 
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20050801/210bc5c2/attachment-0001.htm


More information about the Tutor mailing list