| | |
Projects for the Beginner - Problem with #2
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
![]() |
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).
*to run, save as a .py file and double click it.
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).
python Syntax (Toggle Plain Text)
# 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()
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?
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?

python Syntax (Toggle Plain Text)
# 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()
•
•
Join Date: Dec 2006
Posts: 1,071
Reputation:
Solved Threads: 299
•
•
•
•
Firstly, I can't get the '\n' from the line before putting it in a list.
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.
Last edited by woooee; Feb 2nd, 2008 at 12:22 pm.
![]() |
Similar Threads
- Projects for the Beginner (Python)
- facing problem in database connectivity in jsp to mysql (JSP)
- Programming Problem (C++)
- build problems (C)
- I have a form problem (HTML and CSS)
- New Programmer (aspiring) (C++)
- Equation Solver (Python)
- stuck with definition (C++)
- set me some home work plz (C++)
Other Threads in the Python Forum
- Previous Thread: Built-in GUI kit
- Next Thread: tkinter widget method problem
Views: 1046 | Replies: 6
| Thread Tools | Search this Thread |
Tag cloud for Python
approximation array beginner book builtin change cipher clear client code color converter countpasswordentry cturtle curved def dictionary drive dynamic examples excel file float format ftp function gui homework import inches input java library line lines linux list lists loop microcontroller mouse mysqldb mysqlquery newb number numbers output parsing path plugin port prime program programming projects py2exe pygame pymailer pyqt python random recursion recursive redirect remote script scrolledtext search singleton socket sqlite ssh string strings strip subprocess sum syntax table terminal text textarea thread threading time tkinter tlapse tuple tutorial twoup ubuntu unicode urllib urllib2 variable vigenere wikipedia windows wxpython xlwt






