Projects for the Beginner - Problem with #2

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jan 2008
Posts: 19
Reputation: Begjinner is an unknown quantity at this point 
Solved Threads: 0
Begjinner's Avatar
Begjinner Begjinner is offline Offline
Newbie Poster

Projects for the Beginner - Problem with #2

 
0
  #1
Jan 31st, 2008
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).

  1. # A Code library
  2. # Projects for the Beginner #2
  3. # www.daniweb.com/forums/post159477-2.html
  4. # Python 2.5 tested Begjinner 31jan2008
  5.  
  6. def readLib(lib):
  7. import os
  8. filename = 'codelib.txt'
  9. if os.path.exists(filename):
  10. store = file(filename, 'r')
  11. for line in store:
  12. key = line.strip()
  13. value = line.strip()
  14. lib[key] = value
  15. else:
  16. store = file(filename,'w')
  17. store.close()
  18.  
  19. def saveLib(lib):
  20. store = file("codelib.txt",'w')
  21. for key,value in lib.items():
  22. store.write(key + '\n')
  23. store.write(value + '\n')
  24. store.close()
  25.  
  26. def getOption(menu):
  27. print menu
  28. option = int(raw_input("Select an option(1-4): "))
  29. return option
  30.  
  31. def addEntry(lib):
  32. key = raw_input("Enter a name: ")
  33. value = raw_input("Enter the code: ")
  34. lib[key] = value
  35.  
  36. def removeEntry(lib):
  37. key = raw_input("Enter a name to be deleted: ")
  38. del (lib[key])
  39.  
  40. def findEntry(lib):
  41. key = raw_input("Enter a name: ")
  42. if key in lib.keys():
  43. print key, lib[key]
  44. else: print "Sorry, no entry for: ", key
  45.  
  46. def main():
  47. theMenu = '''
  48. ----------- MENU -----------
  49. 1) Add code
  50. 2) Remove code
  51. 3) Find code
  52.  
  53. 4)Quit and save
  54. '''
  55. theLib = {}
  56. readLib(theLib)
  57. option = getOption(theMenu)
  58. while option != 4:
  59. if option == 1:
  60. addEntry(theLib)
  61. elif option == 2:
  62. removeEntry(theLib)
  63. elif option == 3:
  64. findEntry(theLib)
  65. else: print "Invalid option, try again"
  66. option = getOption(theMenu)
  67. saveLib(theLib)
  68.  
  69.  
  70. main()
*to run, save as a .py file and double click it.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 1,546
Reputation: Ene Uran has a spectacular aura about Ene Uran has a spectacular aura about 
Solved Threads: 174
Ene Uran's Avatar
Ene Uran Ene Uran is offline Offline
Posting Virtuoso

Re: Projects for the Beginner - Problem with #2

 
0
  #2
Jan 31st, 2008
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!
drink her pretty
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 19
Reputation: Begjinner is an unknown quantity at this point 
Solved Threads: 0
Begjinner's Avatar
Begjinner Begjinner is offline Offline
Newbie Poster

Re: Projects for the Beginner - Problem with #2

 
0
  #3
Feb 1st, 2008
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?

  1. # A Code library
  2. # Projects for the Beginner #2
  3. # www.daniweb.com/forums/post159477-2.html
  4. # Python 2.5 tested Begjinner 2feb2008
  5.  
  6. def readLib(lib):
  7. import os
  8. filename = 'codelib.txt'
  9. if os.path.exists(filename):
  10. store = file(filename, 'r')
  11. libList = []
  12. for line in store:
  13. line.strip()
  14. libList.append(line)
  15. for key,value in lib[0::2], lib[1::2]:
  16. theLib[key] = value
  17. else:
  18. store = file(filename,'w')
  19. store.close()
  20.  
  21. def saveLib(lib):
  22. store = file("codelib.txt",'w')
  23. for key,value in lib.items():
  24. store.write(key + '\n')
  25. store.write(value + '\n')
  26. store.close()
  27.  
  28. def getOption(menu):
  29. print menu
  30. option = int(raw_input("Select an option(1-4): "))
  31. return option
  32.  
  33. def addEntry(lib):
  34. key = raw_input("Enter a name: ")
  35. value = raw_input("Enter the code: ")
  36. lib[key] = value
  37.  
  38. def removeEntry(lib):
  39. key = raw_input("Enter a name to be deleted: ")
  40. if key in lib.keys():
  41. del (lib[key])
  42. else: print "Sorry, no entry for: ", key
  43.  
  44. def findEntry(lib):
  45. key = raw_input("Enter a name: ")
  46. if key in lib.keys():
  47. print key, lib[key]
  48. else: print "Sorry, no entry for: ", key
  49.  
  50. def main():
  51. theMenu = '''
  52. ----------- MENU -----------
  53. 1) Add code
  54. 2) Remove code
  55. 3) Find code
  56.  
  57. 4)Quit and save
  58. '''
  59. theLib = {}
  60. readLib(theLib)
  61. option = getOption(theMenu)
  62. while option != 4:
  63. if option == 1:
  64. addEntry(theLib)
  65. elif option == 2:
  66. removeEntry(theLib)
  67. elif option == 3:
  68. findEntry(theLib)
  69. else: print "Invalid option, try again"
  70. option = getOption(theMenu)
  71. saveLib(theLib)
  72.  
  73.  
  74. main()
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,071
Reputation: woooee is a jewel in the rough woooee is a jewel in the rough woooee is a jewel in the rough 
Solved Threads: 299
woooee woooee is offline Offline
Veteran Poster

Re: Projects for the Beginner - Problem with #2

 
0
  #4
Feb 2nd, 2008
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.
Last edited by woooee; Feb 2nd, 2008 at 12:22 pm.
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 146
Reputation: G-Do is an unknown quantity at this point 
Solved Threads: 29
G-Do's Avatar
G-Do G-Do is offline Offline
Junior Poster

Re: Projects for the Beginner - Problem with #2

 
0
  #5
Feb 2nd, 2008
Well, firstly...

(ducks)
Vi veri veniversum vivus vici
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 19
Reputation: Begjinner is an unknown quantity at this point 
Solved Threads: 0
Begjinner's Avatar
Begjinner Begjinner is offline Offline
Newbie Poster

Re: Projects for the Beginner - Problem with #2

 
0
  #6
Feb 9th, 2008
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.
Last edited by Begjinner; Feb 9th, 2008 at 4:52 am.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,071
Reputation: woooee is a jewel in the rough woooee is a jewel in the rough woooee is a jewel in the rough 
Solved Threads: 299
woooee woooee is offline Offline
Veteran Poster

Re: Projects for the Beginner - Problem with #2

 
0
  #7
Feb 9th, 2008
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".
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 1046 | Replies: 6
Thread Tools Search this Thread



Tag cloud for Python
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC