Need help. How do I fix this 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. Save Students"
    print "6. Load Students"
    print "7. Exit"
def save_students(filename,students):
    out_file = open(filename, "w")
    for x in students.keys():
        out_file.write(x+","+students[x]+"\n")
    out_file.close()
def load_students(filename,students):
    in_file = open(filename,"r")
    while 1:
        in_line = in_file.readline()
        if len(in_line) == 0:
            break
        in_line = in_line[-1]
        [name,grade]= string.split(in_line,",")
        grade[name] = grade
    in_file.close()
def print_all_grades():
    print "\t",
    for i in range(len(assignments)):
        print assignments[i],"\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
print_menu()
menu_choice = 0
while menu_choice != 7:
    print
    menu_choice = input("Menu Choice (1-6): ")
    if menu_choice == 1:
        name = raw_input("Student to add: ")
        students[name] = [0]*len(max_points)
    elif menu_choice == 2:
        name = raw_input("Stuent to remove: ")
        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 = raw_input("Student: ")
        if students.has_key(name):
            grades = students[name]
            print "Type the number of the grade to record"
            print "Type 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 = input("Change which Grade")
                which = which-1
                if 0 <= which < len(grades):
                    grade = 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 save? ")
        save_students(filename,students)
    elif menu_choice == 6:
        filename = raw_input("Filename to load")
        load_students(filename,students)
    
    elif menu_choice != 7:
        print_menu()

The instruction from the TUT is this:

Now modify the grades program from section 11 so that is uses file IO to keep a record of the students.

Please help. This is the last hard part.

Recommended Answers

All 3 Replies

Try to modify your save to file function.
Here's what I would do:

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

Is this is what you want?
#Max.... will be also written to a file.

Thanks, could you also help me with the load_student section? The string.split line doesn't work, it says I need more than one parameter to seperate. How should I make that work? I've added the import string statement at the beginning.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.