Thread: Help needed

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    1
    Rep Power
    0

    Help needed


    I have the following code to read file into dictionary, look up the dictionary and modify the file. When appending record to the file, there is always a comma at the end. It doesn't work as expected. Can anyone please help? Thanks!


    max_points = [25,25,50,25,100]
    assignments = ['hw ch 1','hw ch 2','quiz','hw ch 3','test']
    students = {}
    student_list = []

    def savelist():
    outfile = open("VincentStudentRecord.txt","w")
    for i in sorted(students.keys()):
    outfile.write(i+","+str(students[i]).strip("[").strip("]")+"\n")
    outfile.close()

    def readlist():
    in_file = open("VincentstudentRecord.txt","r")
    for i in in_file:
    i = i.strip("\n")
    students[i]=""
    for a in sorted(set(students)):
    in_file.close()

    def print_menu():
    print "1. Add student"
    print "2. Remove student"
    print "3. Print grades"
    print "4. Record grade"
    print "5. Print Menu"
    print "6. Exit"

    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

    readlist()


    print_menu()
    menu_choice = 0
    while menu_choice != 6:
    print
    try:
    menu_choice = int(raw_input("Enter your choice: "))
    except ValueError:
    print "That was not a choice."

    if menu_choice == 1:
    name = raw_input("Student to add:")
    students[name] = [0]*len(max_points)
    student_list.append(name)
    print_all_grades()
    elif menu_choice == 2:
    name = raw_input("Student 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 in 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:

    try:
    which = int(raw_input("Change which grade: "))
    except ValueError:
    print "That was not a choice."

    which = which-1
    if 0 <= which < len(grades):
    try:
    grades = int(raw_input("Grade: "))
    except ValueError:
    print "That was not a proper grade."
    grades[which] = grade
    print_all_grades()
    elif which != -1:
    print "Invalid Grade Number"
    else:
    print "Student not found"
    elif menu_choice != 6:
    print_menu()


    savelist()
  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2012
    Location
    39N 104.28W
    Posts
    158
    Rep Power
    3
    First of all, you should use code tags that preserve indentation.

    As for the problem you state, I think you must have a blank entry in the dictionary. That is, you have a key for which there is no value. When you read the file, the first thing you do is set up all the keys with blank values. Then you have this
    Code:
    for a in sorted(set(students)):
    that seems to do nothing.

    Then you seem to assign values to dictionary entries on an ad hoc basis so it doesn't surprise me that some of the entries might have no value.

    You can avoid writing those records if you want,
    Code:
    if sudents[i]!="": 
        outfile.write(i+","+str(students[i]).strip("[]")+"\n")
    note that a single strip will remove both brackets.

IMN logo majestic logo threadwatch logo seochat tools logo