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 427,316 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 2,850 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: 2247 | Replies: 15
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

Re: Non-programmer tutorial, stuck at list exercise

  #11  
Sep 5th, 2006
Originally Posted by Ene Uran View Post
Since you want to save and later load an entire container object, the module pickle is what you want to use. Hre is a simple example:
# pickle saves and loads any Python object intact
# via a byte stream, use cpickle for higher speed
 
import pickle
 
myList1 = [12, 52, 62, 02, 3.140, "Monte"]
print "Original list:"
print myList1
file = open("list1.dat", "w")
pickle.dump(myList1, file)
file.close()
file = open("list1.dat", "r")
myList2 = pickle.load(file)
file.close()
print "List after pickle.dump() and pickle.load():"
print myList2
In your case you would pickle the students dictionary!


slight issue, when I load the file, the students grades aren't preserved, just the maximum grades. This is the output from loading:

Grades 1 2 3 4 5
#Max 25 25 50 25 100

Not:

Charles 15 24 45 20 90

How do I get the students names and other data, or make sure that it saves them?
Reply With Quote  
Join Date: Oct 2004
Location: Mojave Desert
Posts: 2,476
Reputation: vegaseat will become famous soon enough vegaseat will become famous soon enough 
Rep Power: 10
Solved Threads: 176
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
Kickbutt Moderator

Re: Non-programmer tutorial, stuck at list exercise

  #12  
Sep 5th, 2006
What does your student dictionary look like just before you pickle.dump() it?
Also, you could write a small Python program that pickle.load() that file and looks at its contents.
May 'the Google' be with you!
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

Help Re: Non-programmer tutorial, stuck at list exercise

  #13  
Sep 5th, 2006
Originally Posted by vegaseat View Post
What does your student dictionary look like just before you pickle.dump() it?
Also, you could write a small Python program that pickle.load() that file and looks at its contents.


#Max 25 25 50 25 100
Becky 9 20 30 16 57
Charles 15 20 50 25 98
Steve 25 14 45 24 19

That's what it looks like. This is what happens:

#Max 25 25 50 25 100

It just erases the students data when pickle dumps it.
Reply With Quote  
Join Date: Aug 2005
Posts: 1,135
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: Non-programmer tutorial, stuck at list exercise

  #14  
Sep 5th, 2006
Watch out when you start your program that your basic students dictionary does not override the newly loaded dictionary. Here is your code sample that works:
import pickle

max_points = [25,25,50,25,100]
assignments = ["hw ch 1","hw ch 2","quiz   ","hw ch 3","test"]

try:
    file = open("students.dat", "r")
    students = pickle.load(file)
    file.close()
except:
    students = {"#Max":max_points}
    print "students.dat file not found, starting with", students

def print_menu():
    print "1. Add student"
    print "2. Remove student"
    print "3. Print grades"
    print "4. Record grade"
    print "5. 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
    
print_menu()
menu_choice = 0
while menu_choice != 5:
    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("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 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:
        print_menu()

print students  # test
# update students.dat file upon exit
file = open("students.dat", "w")
pickle.dump(students, file)
file.close()
Last edited by Ene Uran : Sep 5th, 2006 at 12:28 pm.
drink her pretty
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: Non-programmer tutorial, stuck at list exercise

  #15  
Sep 5th, 2006
Originally Posted by Ene Uran View Post
Watch out when you start your program that your basic students dictionary does not override the newly loaded dictionary. Here is your code sample that works:
import pickle
 
max_points = [25,25,50,25,100]
assignments = ["hw ch 1","hw ch 2","quiz   ","hw ch 3","test"]
 
try:
    file = open("students.dat", "r")
    students = pickle.load(file)
    file.close()
except:
    students = {"#Max":max_points}
    print "students.dat file not found, starting with", students
 
def print_menu():
    print "1. Add student"
    print "2. Remove student"
    print "3. Print grades"
    print "4. Record grade"
    print "5. 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
 
print_menu()
menu_choice = 0
while menu_choice != 5:
    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("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 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:
        print_menu()
 
print students  # test
# update students.dat file upon exit
file = open("students.dat", "w")
pickle.dump(students, file)
file.close()


So this code will auto save it? Thanks. Now to the final chapter: Error checking! .
Reply With Quote  
Join Date: Aug 2005
Posts: 1,135
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: Non-programmer tutorial, stuck at list exercise

  #16  
Sep 6th, 2006
Good! The try/except is part of error checking you will tackle next.

What it does in the above code is the following, it tries to load the data file, if there is an error like a missing data file, it does the except part. In the except part you can be more specific about the exact error. Well anyway, have fun learning Python.
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

Other Threads in the Python Forum

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