User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Python section within the Software Development category of DaniWeb, a massive community of 423,548 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,909 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Python advertiser: Programming Forums
Views: 794 | Replies: 3
Reply
Join Date: Sep 2006
Posts: 104
Reputation: chris99 is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 0
chris99's Avatar
chris99 chris99 is offline Offline
Junior Poster

Trying to save the info in the student library.

  #1  
Sep 4th, 2006
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.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Aug 2005
Location: Bosnia and Herzegovina
Posts: 147
Reputation: Micko is on a distinguished road 
Rep Power: 4
Solved Threads: 1
Micko Micko is offline Offline
Junior Poster

Re: Trying to save the info in the student library.

  #2  
Sep 4th, 2006
Try to modify your save to file function.
Here's what I would do:
[php]
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()
[/php]

Is this is what you want?
#Max.... will be also written to a file.
Reply With Quote  
Join Date: Sep 2006
Posts: 104
Reputation: chris99 is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 0
chris99's Avatar
chris99 chris99 is offline Offline
Junior Poster

Re: Trying to save the info in the student library.

  #3  
Sep 4th, 2006
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.
Reply With Quote  
Join Date: Aug 2005
Posts: 1,118
Reputation: Ene Uran is an unknown quantity at this point 
Rep Power: 6
Solved Threads: 66
Ene Uran's Avatar
Ene Uran Ene Uran is offline Offline
Veteran Poster

Re: Trying to save the info in the student library.

  #4  
Sep 4th, 2006
drink her pretty
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Python Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the Python Forum

All times are GMT -4. The time now is 6:00 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC