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

Nathan Pinno falcon3166 at hotmail.com
Wed Aug 3 20:59:53 CEST 2005

Here is the latest code and error:
Traceback (most recent call last):
  File "D:\Python24\grades.py", line 99, in -toplevel-
  File "D:\Python24\grades.py", line 51, in save_grades
TypeError: unsupported operand type(s) for +: 'int' and 'str'

max_points = [25,25,50,25,100]
assignments = ['hw ch 1','hw ch 2','quiz   ','hw ch 3','test']
students = {1: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',
    keys = students.keys()
    for x in keys:
        print x,'\t',
        grades = students[x]

def print_grades(grades):
    for i in range(len(grades)):
        print grades[i],'\t\t',

def choice():
    return int(raw_input("Menu Choice: "))

def school():
    return int(raw_input("Student ID: "))

def load_grades(students,filename):
    in_file = open(filename, "r")
    while 1:
        in_line = in_file.readline()
        if in_line == "":
        in_line = in_line[:-1]
        [students,max_points] = string.split(in_line,",")
        max_points[students] = grade

def save_grades(students,filename):
    out_file = open(filename, "w")
    for x in students.keys():

print "Grade Tracking Program."
while 1:
    menu_choice = choice()
    if menu_choice == 1:
        print "Add student"
        ID = school()
        students[ID] = [0]*len(max_points)
    elif menu_choice == 2:
        print "Remove student"
        ID = school()
        if students.has_key(ID):
            del students[ID]
            print "Student ID: ",ID," not found."
    elif menu_choice == 3:

    elif menu_choice == 4:
        print "Record Grade"
        ID = school()
        if students.has_key(ID):
            grades = students[ID]
            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',
            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"
                print "Student not found"
    elif menu_choice == 5:
        filename = raw_input("Filename to load: ")
    elif menu_choice == 6:
        filename = raw_input("Filename to save: ")
    elif menu_choice == 9:
        print "That's not a choice!"
print "Goodbye."

How can I fix it?
----- Original Message ----- 
From: "Nathan Pinno" <falcon3166 at hotmail.com>
To: "Alan G" <alan.gauld at freenet.co.uk>; "Yoo, Danny" 
<dyoo at hkn.eecs.berkeley.edu>; "Bark, Adam" <adam.jtm30 at gmail.com>
Cc: "Tutor" <tutor at python.org>
Sent: Wednesday, August 03, 2005 12:42 PM
Subject: Re: [Tutor] I've run into a jam on the exercise on file I/O

> I've been chatting with Albertito on MSN and came up with this solution:
> int(x), and change student into student ids, and remove #Max. Will this
> work, or will I need to add sequential IDs?
> ----- Original Message ----- 
> From: "Alan G" <alan.gauld at freenet.co.uk>
> To: "Nathan Pinno" <falcon3166 at hotmail.com>; "Yoo, Danny"
> <dyoo at hkn.eecs.berkeley.edu>; "Bark, Adam" <adam.jtm30 at gmail.com>
> Cc: "Tutor" <tutor at python.org>
> Sent: Tuesday, August 02, 2005 1:43 AM
> Subject: Re: [Tutor] I've run into a jam on the exercise on file I/O
>>> What if I were to use ID's for the students and use the ID's as the
>>> sequence
>>> index, and link the students and their grades to the IDs?
>> Its not necessary for what you are doing but it's how you'd normally do 
>> it
>> in a relational database, so if you ever needed to turn your file based
>> program into a full blown database then using IDs would actually help!
>> The other thing you could do is hold all the data for a single student
>> in one structure. Thus instead of having lots of lists all linked by
>> a common index (or ID) you have one collection of students (probably
>> a dictionary keyed by name) each with all of its own data. This
>> involves a more complex data structure and is a move in the direction
>> of object oriented programming but without actual classes and objects
>> being involved. This might also allow you to use the shelve moduile
>> to save/restore your data to file easily.
>> Just a thought,
>> Alan G.
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor

More information about the Tutor mailing list