Hello,

I'm doing (post) #2 of 'Projects for the Beginner' and I used for making this the 'The Address Book Revisited' example on this page The author says he even put in two errors on purpose. I have re-written the code and changed it more to a Code Library like program, but I have a problem with reading the file correctly into the dictionary.

So the problem is in the function readLib(lib), the for-construction is not correct used.

I have included the code below.

Visitors are free to use my code, but it's not 100% good (yet).

# A Code library
# Projects for the Beginner #2
# www.daniweb.com/forums/post159477-2.html
# Python 2.5 tested      Begjinner       31jan2008

def readLib(lib): 
    import os
    filename = 'codelib.txt'
    if os.path.exists(filename):
        store = file(filename, 'r')
        for line in store:
            key = line.strip()
            value = line.strip()
            lib[key] = value
    else:
        store = file(filename,'w')
    store.close()

def saveLib(lib): 
    store = file("codelib.txt",'w')
    for key,value in lib.items():
        store.write(key + '\n')
        store.write(value + '\n')
    store.close()

def getOption(menu):
    print menu
    option = int(raw_input("Select an option(1-4): "))
    return option
    
def addEntry(lib):
    key = raw_input("Enter a name: ")
    value = raw_input("Enter the code: ")
    lib[key] = value
    
def removeEntry(lib):
    key = raw_input("Enter a name to be deleted: ")
    del (lib[key])
    
def findEntry(lib):
    key = raw_input("Enter a name: ")
    if key in lib.keys():
        print key, lib[key]        
    else: print "Sorry, no entry for: ", key

def main():
    theMenu = '''
    ----------- MENU -----------
    1) Add code
    2) Remove code
    3) Find code
    
    4)Quit and save
    '''
    theLib = {}
    readLib(theLib)
    option = getOption(theMenu)
    while option != 4:
        if option == 1:
            addEntry(theLib)
        elif option == 2:
            removeEntry(theLib)
        elif option == 3:
            findEntry(theLib)
        else: print "Invalid option, try again"
        option = getOption(theMenu)
    saveLib(theLib)
    

main()

*to run, save as a .py file and double click it.

Recommended Answers

All 6 Replies

Nice work, but the code input is limited to one line. Might have to put that one into a while loop with a break key symbol to end the loop.

Happy coding!

I updated the code (see below) with an if statement in the removeEntry function, but the readLib function is still buggy.

Firstly, I can't get the '\n' from the line before putting it in a list. Google-ed it with no answer.

Secondly, the for loop used to create the dictionary in the readLib function doesn't work. Python says in test script "ValueError: too many values to unpack" and in total script "TypeError: unhashable type".

Help? :(

# A Code library
# Projects for the Beginner #2
# www.daniweb.com/forums/post159477-2.html
# Python 2.5 tested      Begjinner       2feb2008

def readLib(lib): 
    import os
    filename = 'codelib.txt'
    if os.path.exists(filename):
        store = file(filename, 'r')
        libList = []
        for line in store:
            line.strip()
            libList.append(line)
        for key,value in lib[0::2], lib[1::2]:
            theLib[key] = value
    else:
        store = file(filename,'w')
    store.close()

def saveLib(lib): 
    store = file("codelib.txt",'w')
    for key,value in lib.items():
        store.write(key + '\n')
        store.write(value + '\n')
    store.close()

def getOption(menu):
    print menu
    option = int(raw_input("Select an option(1-4): "))
    return option
    
def addEntry(lib):
    key = raw_input("Enter a name: ")
    value = raw_input("Enter the code: ")
    lib[key] = value
    
def removeEntry(lib):
    key = raw_input("Enter a name to be deleted: ")
    if key in lib.keys():
        del (lib[key])
    else: print "Sorry, no entry for: ", key
    
def findEntry(lib):
    key = raw_input("Enter a name: ")
    if key in lib.keys():
        print key, lib[key]        
    else: print "Sorry, no entry for: ", key

def main():
    theMenu = '''
    ----------- MENU -----------
    1) Add code
    2) Remove code
    3) Find code
    
    4)Quit and save
    '''
    theLib = {}
    readLib(theLib)
    option = getOption(theMenu)
    while option != 4:
        if option == 1:
            addEntry(theLib)
        elif option == 2:
            removeEntry(theLib)
        elif option == 3:
            findEntry(theLib)
        else: print "Invalid option, try again"
        option = getOption(theMenu)
    saveLib(theLib)
    

main()

Firstly, I can't get the '\n' from the line before putting it in a list.

I don't think "firstly" is a word, but I'm not sure.
var=var.strip()
will remove all whitespace before and after the text for the variable var. Whitespace is spaces, tabs, newlines, etc. As for the "too many values to unpack", it probably refers to this line
for key,value in lib[0::2], lib[1::2]:
but it is impossible to tell without the entire error message. It doesn't matter though since you will have to print some things to find out anyway. Start by putting these before the aforementioned for statement
print "lib[0] =", lib[0]
print "lib[1] =", lib[1]
print "lib[0::2], lib[1::2] =", lib[0::2], lib[1::2]
Hopefully that will give you some hints about what comes after the "in" in the for statement.

the strip() now works, but I can't get that "for key,value..." function working. The hints did not help...

I think I'll put this project on a stop.

Don't give up, find another way to do it. Worst case is that you post the code for the function readLib(lib), and the contents of lib, or part of the contents if it is large, and ask in specific words "how you can code it to do what you want it to do".

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.