[Tutor] How do I fix this ValueError?

Alan G alan.gauld at freenet.co.uk
Wed Aug 10 05:50:11 CEST 2005


>     for site,ID,passcard in sitelist.items():
> ValueError: need more than 2 values to unpack

The line is asking a dictionary (sitelist) to return 3 values
(site, ID and passcard) from its items() method. But A dictionary
stores things as name/value pairs so you can only get 2 items
back not 3.

Yopu need to change the code to read the key/value from the 
and then, unpack the value data into the individual pieces.

def save_file(pw):
    store = open('passcard.txt',"w")
    for site,ID,passcard in sitelist.items():
        store.write(site + '\n')
        store.write(ID + '\n')
        store.write(passcard + '\n')

def add_site():
    print "Add a login info card"
    site = raw_input("Site: ")
    ID = raw_input("User ID: ")
    passcard = getpass.getpass("Password: ")
    sitelist[site] = [ID,passcard]

You see here? you store ID and passcard as a list, so items()
will return that list alomng with the key. You need to unpick
them from the list.

You might be better investigating the shelve module which
allows you to store a dictionaruy to a file and access it
as if it were still a dictionary. Shelve would be pretty
much ideal for this application.

Alan G
Author of the Learn to Program web tutor

More information about the Tutor mailing list