I add a new name to this script, but when I want to quit and save
it gives and error

ValueError: need more than 2 values to unpack

I have no idea what it means can someone help me?
If you want to modify the program feel free...

filename = "movielog.dat"

def readBook(book):

	import os
	if os.path.exists(filename):
		store = open(filename, 'r')
		for line in store:
			name = line.rstrip()
			year = store.next().rstrip() 
			roles = store.next().rstrip()
			book[name] = year, roles
		store.close()

def saveBook(book):
	store = open(filename, 'w')
	for name,year,roles in book.items():
		store.write(name + '\n')
		store.write(year + '\n')
		store.write(roles + '\n')
	store.close()

def getChoice(menu):
        print menu
        choice = int(raw_input("Select a choice(1-4): ") )
        return choice

def addEntry(book):
        name = raw_input("Enter a name: ")
        year = raw_input("Enter year and director: ")
        roles = raw_input("Enter leading roles (male and female): ") 
        book[name] = year, roles

def removeEntry(book):
        name = raw_input("Enter a name: ")
        del(book[name])

def findEntry(book):
        name = raw_input("Enter a name: ")
        if name in book:
                print name, book[name]
        else:
                print "Sorry, no entry for: ", name

def main():
        theMenu = '''
        1) Add entry
        2) Remove entry
        3) Find entry
        4) Quit and save
        '''
        theBook = {}
        readBook(theBook)
        choice = getChoice(theMenu)
        while choice != 4:
                if choice == 1:
                        addEntry(theBook)
                elif choice == 2:
                        removeEntry(theBook)
                elif choice == 3:
                        findEntry(theBook)
                else : print "Invalid choice, try again"
                choice = getChoice(theMenu)
        saveBook(theBook)

if __name__ == "__main__":
        main()

Your problem is that book.items() gives you a key, value pair where value is most likely a tuple (year, roles). You have to edit your function like this ...

def saveBook(book):
    store = open(filename, 'w')
    for name, val in book.items():
        year, roles = val 
        store.write(name + '\n')
        store.write(year + '\n')
        store.write(roles + '\n')
    store.close()

I don't have your data file, so I can't test it!

I just started messing around with Python the other day...this may be the first program I've compiled over 10 lines that wasn't a straight copy and paste from the online help.

I believe the problem was "book" only had 2 elements in it (not 3). The second element was a list making book a 2d array.

Anyway, I'm sure there is a more elegant way of doing this (probably much like what you had tried), but here is one solution:

filename = "movielog.dat"

def readBook(book):

	import os
	if os.path.exists(filename):
		store = open(filename, 'r')
		for line in store:
			name = line.rstrip()
			year = store.next().rstrip() 
			roles = store.next().rstrip()
			book[name] = year, roles
		store.close()

def saveBook(book):
	store = open(filename, 'w')
	for name in book:
		store.write(name + '\n')
		store.write(book[name][0] + '\n')
		store.write(book[name][1] + '\n')
	store.close()

def getChoice(menu):
        print menu
        choice = int(raw_input("Select a choice(1-5): ") )
        return choice

def addEntry(book):
        name = raw_input("Enter a name: ")
        year = raw_input("Enter year and director: ")
        roles = raw_input("Enter leading roles (male and female): ") 
        book[name] = year, roles

def removeEntry(book):
        name = raw_input("Enter a name: ")
        del(book[name])

def findEntry(book):
        name = raw_input("Enter a name: ")
        if name in book:
                print name, book[name]
        else:
                print "Sorry, no entry for: ", name
def dispEntry(book):
    i = 1
    print '\n\nBook entries:\n\n','-'*80, '\nEntry #'.rjust(8), 'Name'.rjust(20), 'Year'.rjust(20), 'Roles'.rjust(20),'\n','-'*80
    for name in book:
        print str(i).rjust(3),':', name.rjust(23),book[name][0].rjust(20),book[name][1].rjust(20)
        i += 1
        
def main():
        theMenu = '''
        1) Add entry
        2) Remove entry
        3) Find entry
        4) Display entrys
        5) Quit and save
        '''
        theBook = {}
        readBook(theBook)
        choice = getChoice(theMenu)
        while choice != 5:
                if choice == 1:
                        addEntry(theBook)
                elif choice == 2:
                        removeEntry(theBook)
                elif choice == 3:
                        findEntry(theBook)
                elif choice == 4:
                        dispEntry(theBook)
                else : print "Invalid choice, try again"
                choice = getChoice(theMenu)
        saveBook(theBook)

if __name__ == "__main__":
        main()

I was reading about the pickle module and tried to apply it, but for some reason I couldn't get it to load the data correctly.

You can only load data with pickle that has been saved/dumped with pickle, since it saves the entire object as a byte stream, in your case the object would be the book dictionary. Here is an example ...

# pickle saves and loads any Python object intact
# via a byte stream
# use module cPickle for higher speed

import pickle

myList1 = [12, 52, 62, "Monte"]
print "Original list:"
print myList1

fout = open("list1.dat", "w")
pickle.dump(myList1, fout)
fout.close()

fin = open("list1.dat", "r")
myList2 = pickle.load(fin)
fin.close()

print "List after pickle.dump() and pickle.load():"
print myList2

You could initially put in the pickle dump as a save option and then switch totally to pickle load and dump.

Wel thanks for your help
I have solved it with the thread of vegaseat.
Thanks a lot

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.