[Tutor] How do I fix this ValueError?

Nathan Pinno falcon3166 at hotmail.com
Wed Aug 10 02:38:52 CEST 2005


Here is the error:

Traceback (most recent call last):
  File "D:\Python24\password.py", line 91, in -toplevel-
    save_file(sitelist)
  File "D:\Python24\password.py", line 22, in save_file
    for site,ID,passcard in sitelist.items():
ValueError: need more than 2 values to unpack

Here is the code:

#This is for a password protected program to store passwords.
import getpass
password = "hello"
sitelist = {}

def load_file(pw):
    import os
    filename = 'passcard.txt'
    if os.path.exists(filename):
       store = open(filename,'r')
       for line in store:
          site = line.strip()
          ID = line.strip()
          passcard = store.next().strip()
          pw[site] = ID and passcard 
    else:
        store = open(filename,'w') # create new empty file
    store.close()

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')
    store.close()


def main_menu():
    print "1) Add a login info card"
    print "2) Lookup a login info card"
    print "3) Remove a login info card"
    print "4) Print Login info list"
    print "9) Save and Exit"

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]

def lookup_site():
    print "Lookup a login info card"
    site = raw_input("Site: ")
    if sitelist.has_key(site):
        print "The ID is: ",sitlist[site][0]
        print "The password is: ",sitelist[site][1]
    else:
        print site," was not found."

def remove_site():
     print "Remove a login info card"
     site = raw_input("Site: ")
     if sitelist.has_key(site):
         del sitelist[site]
     else:
         print site," was not found."

def print_login_info():
    print "Login Info"
    for site in sitelist.keys():
        print "Site: ",site," \tID: ",sitelist[site][0]," \tPassword: ",sitelist[site][1],"\n"


print "The Password Program"
print "By Nathan Pinno"
print
load_file(sitelist)
answer = getpass.getpass("What is the password? ")
while password != answer:
    print "The password is incorrect."
    answer = getpass.getpass("What is the password? ")

print "Welcome to the second half of the program."
while 1:
    main_menu()
    menu_choice = int(raw_input("Choose an option (1-4, or 9: "))
    if menu_choice == 1:
        add_site()
    elif menu_choice == 2:
        lookup_site()
    elif menu_choice == 3:
        remove_site()
    elif menu_choice == 4:
        print_login_info()
    elif menu_choice == 9:
        break
    else:
        print "That's not an option!"
save_file(sitelist)
print "Have a nice day!"

How do I fix the program, so that it will save the info without raising any errors?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20050809/9c9e14e1/attachment-0001.htm
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Pinno, Nathan Paul.vcf
Type: text/x-vcard
Size: 783 bytes
Desc: not available
Url : http://mail.python.org/pipermail/tutor/attachments/20050809/9c9e14e1/PinnoNathanPaul-0001.vcf


More information about the Tutor mailing list