Adress book

Reply

Join Date: Jun 2009
Posts: 28
Reputation: Your_mum is an unknown quantity at this point 
Solved Threads: 0
Your_mum Your_mum is offline Offline
Light Poster

Adress book

 
0
  #1
Jun 28th, 2009
Hi attempting to make an address book program but hit a snag-
I have:
A class to give various attributes(phone numbers etc)to an instance
which then puts it all into a doc string which can be added to a dictionary-
but I want a function that will ask the user for the relevant information and then feed it to the class as the relevant arguments. however I Can't figure that out. Any Ideas-Section of code below:
  1. class Data:
  2. def __init__(self, home, mobile, email):
  3. self.home = home
  4. self.mobile = mobile
  5. self.email = email
  6. def Coll_Data(self):
  7. self.info = """
  8. Home Phone: %s
  9. Mobile Phone: %s
  10. Email: %s
  11. """ % (self.home, self.mobile, self.email)
  12.  
  13. #the bit that doesn't work that needs changing is below:
  14.  
  15. def add_new():
  16. name = raw_input("Enter name to be added: ")
  17. h = raw_input("Enter home phone: ")
  18. m = raw_input("Enter Mobile phone: ")
  19. e = raw_input("Enter Email address: ")
  20. name = Data(h, m, e)
Please help!!!!!!
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,275
Reputation: sneekula has a spectacular aura about sneekula has a spectacular aura about 
Solved Threads: 175
sneekula's Avatar
sneekula sneekula is offline Offline
Nearly a Posting Maven

Re: Adress book

 
0
  #2
Jun 28th, 2009
Oh I see, you want the instance name be the name of the person. That will not be a good idea, because identifiers in Python don't allow spaces.

A dictionary, where the name is the key and the value is a ( home, mobile, email ) tuple or list will do much better. You could also make the class instance the value of your dictionary.
Last edited by sneekula; Jun 28th, 2009 at 11:29 am.
No one died when Clinton lied.
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 28
Reputation: Your_mum is an unknown quantity at this point 
Solved Threads: 0
Your_mum Your_mum is offline Offline
Light Poster

Re: Adress book

 
0
  #3
Jun 28th, 2009
Thanks, but making the class instance the value of the dictionary does not work it just returns the location of the instance in hexadecimal notation.

I have rejigged the code:
  1. class Data:
  2. def __init__(self):
  3. self.home = raw_input("Enter home: ")
  4. self.mobile = raw_input("Enter mobile: ")
  5. self.email = raw_input("Enter email :")
  6. def Coll_data(self):
  7. self.info = """
  8. Home : %s
  9. Mobile : %s
  10. Email : %s
  11. """ % (self.home, self.mobile, self.email)
I intend to add it to the dictionary using self.info as the value.
But I really need a function that will create a variable and asign it to class Data.
Last edited by Your_mum; Jun 28th, 2009 at 11:53 am. Reason: typo
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,275
Reputation: sneekula has a spectacular aura about sneekula has a spectacular aura about 
Solved Threads: 175
sneekula's Avatar
sneekula sneekula is offline Offline
Nearly a Posting Maven

Re: Adress book

 
0
  #4
Jun 28th, 2009
This works just fine:
  1. # create a dictionary of class instance values
  2.  
  3. class Data(object):
  4. def __init__(self, home, mobile, email):
  5. self.home = home
  6. self.mobile = mobile
  7. self.email = email
  8. self.get_info()
  9.  
  10. def get_info(self):
  11. sf = \
  12. """Home Phone: %s
  13. Mobile Phone: %s
  14. Email: %s
  15. """
  16. return sf % (self.home, self.mobile, self.email)
  17.  
  18.  
  19. name_dic = {}
  20.  
  21. # first set of data
  22. name = 'frank'
  23. home = '123-456-7890'
  24. mobile = '456-789-0123'
  25. email = 'frank23@gmail.com'
  26.  
  27. name_dic[name] = Data(home, mobile, email)
  28.  
  29. # second set of data
  30. name = 'bob'
  31. home = '234-567-8901'
  32. mobile = '567-690-1234'
  33. email = 'bob.dork@hotmail.com'
  34.  
  35. name_dic[name] = Data(home, mobile, email)
  36.  
  37. # get bob's email
  38. name = 'bob'
  39. print( name_dic[name].email )
  40.  
  41. print('-'*30)
  42.  
  43. # get the info for all names added so far
  44. for key in name_dic:
  45. print( "Name = %s" % key )
  46. print( name_dic[key].get_info() )
  47.  
  48. """
  49. my output -->
  50. bob.dork@hotmail.com
  51. ------------------------------
  52. Name = frank
  53. Home Phone: 123-456-7890
  54. Mobile Phone: 456-789-0123
  55. Email: frank23@gmail.com
  56.  
  57. Name = bob
  58. Home Phone: 234-567-8901
  59. Mobile Phone: 567-690-1234
  60. Email: bob.dork@hotmail.com
  61. """
Remember that the name string is the dictionary key and has to be unique.
Last edited by sneekula; Jun 28th, 2009 at 12:56 pm.
No one died when Clinton lied.
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 28
Reputation: Your_mum is an unknown quantity at this point 
Solved Threads: 0
Your_mum Your_mum is offline Offline
Light Poster

Re: Adress book

 
0
  #5
Jun 28th, 2009
Thanks
that was extremely helpful
Last edited by Your_mum; Jun 28th, 2009 at 1:30 pm.
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 28
Reputation: Your_mum is an unknown quantity at this point 
Solved Threads: 0
Your_mum Your_mum is offline Offline
Light Poster

Re: Adress book

 
0
  #6
Jun 29th, 2009
Using the code you gave me, I modified it a bit to enable more easy user interaction, but I want a function to change an attribute of an entry, say the email.
heres the code so far
  1. intro = "Welcome to Adress_Book.py"
  2. name_dic = {}
  3. loop = 1
  4. menu_choice = 0
  5.  
  6. class Data(object):
  7. def __init__(self, home, mobile, email):
  8. self.home = home
  9. self.mobile = mobile
  10. self.email = email
  11. self.get_info()
  12.  
  13. def get_info(self):
  14. sf = \
  15. """Home Phone: %s
  16. Mobile Phone: %s
  17. Email: %s
  18. """
  19. return sf % (self.home, self.mobile, self.email)
  20.  
  21. def add_new():
  22. name = raw_input("Enter name: ")
  23. home = raw_input("Enter home: ")
  24. mobile = raw_input("Enter mobile: ")
  25. email = raw_input("Enter email: ")
  26.  
  27. name_dic[name] = Data(home, mobile, email)
  28.  
  29. def printout():
  30. for key in name_dic:
  31. print( "Name = %s" % key )
  32. print( name_dic[key].get_info() )
  33.  
  34. def look_up():
  35. search = raw_input("Enter name to be searched: ")
  36. if name_dic.has_key(search):
  37. print( "Name = %s" % search )
  38. print( name_dic[search].get_info() )
  39.  
  40. else:
  41. print( "Name %s not found" % search )
  42.  
  43. def del_entry():
  44. d_search = raw_input("Enter name to be deleted: ")
  45. if name_dic.has_key(d_search):
  46. del name_dic[d_search]
  47. print( "Name %s deleted" % d_search)
  48.  
  49. else:
  50. print( "Name %s not found" % d_search )
  51.  
  52. def change_att():
  53. c_search = raw_input("Enter name to alter: ")
  54. if name_dic.has_key(c_search):
  55. c_name = raw_input("What element do you want to change: ")
  56. if c_name == 'email':
  57. n_e = raw_input("Enter new email adress: ")
  58. #n_e being the new email adress
so how can I take n_e and swap it for the old email adress
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 28
Reputation: Your_mum is an unknown quantity at this point 
Solved Threads: 0
Your_mum Your_mum is offline Offline
Light Poster

Re: Adress book

 
0
  #7
Jun 29th, 2009
sorry my mistake
  1. intro = "Welcome to Adress_Book.py"
  2. name_dic = {}
  3. loop = 1
  4. menu_choice = 0
  5.  
  6. class Data(object):
  7. def __init__(self, home, mobile, email):
  8. self.home = home
  9. self.mobile = mobile
  10. self.email = email
  11. self.get_info()
  12.  
  13. def get_info(self):
  14. sf = \
  15. """Home Phone: %s
  16. Mobile Phone: %s
  17. Email: %s
  18. """
  19. return sf % (self.home, self.mobile, self.email)
  20.  
  21. def add_new():
  22. name = raw_input("Enter name: ")
  23. home = raw_input("Enter home: ")
  24. mobile = raw_input("Enter mobile: ")
  25. email = raw_input("Enter email: ")
  26.  
  27. name_dic[name] = Data(home, mobile, email)
  28.  
  29. def printout():
  30. for key in name_dic:
  31. print( "Name = %s" % key )
  32. print( name_dic[key].get_info() )
  33.  
  34. def look_up():
  35. search = raw_input("Enter name to be searched: ")
  36. if name_dic.has_key(search):
  37. print( "Name = %s" % search )
  38. print( name_dic[search].get_info() )
  39.  
  40. else:
  41. print( "Name %s not found" % search )
  42.  
  43. def del_entry():
  44. d_search = raw_input("Enter name to be deleted: ")
  45. if name_dic.has_key(d_search):
  46. del name_dic[d_search]
  47. print( "Name %s deleted" % d_search)
  48.  
  49. else:
  50. print( "Name %s not found" % d_search )
  51.  
  52. def change_att():
  53. c_search = raw_input("Enter name to alter: ")
  54. if name_dic.has_key(c_search):
  55. c_name = raw_input("What element do you want to change: ")
  56. if c_name == 'email':
  57. n_e = raw_input("Enter new email adress: ")
  58. name_dic[c_search].email = n_e
  59. elif c_name == 'home':
  60. n_h = raw_input("Enter new home number: ")
  61. name_dic[c_search].home = n_h
  62. elif c_name == 'mobile':
  63. n_m = raw_input("Enter new mobile number: ")
  64. name_dic[c_search].mobile = n_m
  65. else:
  66. print( "Name %s not found" % c_search )
  67.  
  68. print intro
  69.  
  70. while loop != 0:
  71. print """
  72. 1) Add new entry
  73. 2) Remove an entry
  74. 3) Look up an entry
  75. 4) Print out adress book
  76. 5) Edit an entry
  77. 6) Exit
  78. """
  79.  
  80. menu_choice = input("Enter choice: ")
  81.  
  82. if menu_choice == 1:
  83. add_new()
  84. elif menu_choice == 2:
  85. del_entry()
  86. elif menu_choice == 3:
  87. look_up()
  88. elif menu_choice == 4:
  89. printout()
  90. elif menu_choice == 5:
  91. change_att()
  92. elif menu_choice == 6:
  93. loop = 0
  94. else:
  95. print "Please anter a valid option"
I want to write file I/O for this, having tried before and failed, so could someone pls point me in the right direction.
Last edited by Your_mum; Jun 29th, 2009 at 2:18 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,275
Reputation: sneekula has a spectacular aura about sneekula has a spectacular aura about 
Solved Threads: 175
sneekula's Avatar
sneekula sneekula is offline Offline
Nearly a Posting Maven

Re: Adress book

 
0
  #8
Jun 29th, 2009
Let's say you want to change the email:
  1. # a test setup
  2.  
  3. class Data(object):
  4. def __init__(self, home, mobile, email):
  5. self.home = home
  6. self.mobile = mobile
  7. self.email = email
  8. self.get_info()
  9.  
  10. def get_info(self):
  11. sf = \
  12. """Home Phone: %s
  13. Mobile Phone: %s
  14. Email: %s
  15. """
  16. return sf % (self.home, self.mobile, self.email)
  17.  
  18.  
  19. name_dic = {}
  20.  
  21. # first set of data for testing
  22. name = 'frank millato'
  23. home = '123-456-7890'
  24. mobile = '456-789-0123'
  25. email = 'frank23@gmail.com'
  26. name_dic[name] = Data(home, mobile, email)
  27.  
  28. # second set of data for testing
  29. name = 'bob dork'
  30. home = '234-567-8901'
  31. mobile = '567-690-1234'
  32. email = 'bob.dork@hotmail.com'
  33. name_dic[name] = Data(home, mobile, email)
  34.  
  35. def change_att():
  36. #c_search = raw_input("Enter name to alter: ")
  37. c_search = 'bob dork' # for testing only!!!
  38. if name_dic.has_key(c_search):
  39. #c_name = raw_input("What element do you want to change: ")
  40. c_name = 'email' # for tesing only!!!
  41. if c_name == 'email':
  42. #n_e = raw_input("Enter new email adress: ")
  43. n_e = 'bob.dork@yahoo.com' # for testing only!!!
  44. # make the change
  45. k = name_dic[c_search]
  46. name_dic[c_search] = Data(k.home, k.mobile, n_e)
  47.  
  48. # get the info for all names added before change
  49. for key in name_dic:
  50. print( "Name = %s" % key )
  51. print( name_dic[key].get_info() )
  52.  
  53. print('-'*40)
  54.  
  55. change_att()
  56.  
  57. # get the info for all names added after change
  58. for key in name_dic:
  59. print( "Name = %s" % key )
  60. print( name_dic[key].get_info() )
  61.  
  62. """
  63. my output -->
  64. Name = frank millato
  65. Home Phone: 123-456-7890
  66. Mobile Phone: 456-789-0123
  67. Email: frank23@gmail.com
  68.  
  69. Name = bob dork
  70. Home Phone: 234-567-8901
  71. Mobile Phone: 567-690-1234
  72. Email: bob.dork@hotmail.com
  73.  
  74. ----------------------------------------
  75. Name = frank millato
  76. Home Phone: 123-456-7890
  77. Mobile Phone: 456-789-0123
  78. Email: frank23@gmail.com
  79.  
  80. Name = bob dork
  81. Home Phone: 234-567-8901
  82. Mobile Phone: 567-690-1234
  83. Email: bob.dork@yahoo.com
  84. """
No one died when Clinton lied.
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 28
Reputation: Your_mum is an unknown quantity at this point 
Solved Threads: 0
Your_mum Your_mum is offline Offline
Light Poster

Re: Adress book

 
0
  #9
Jun 29th, 2009
cheers, but do you have any ideas on pickleing this to create file io
especially regarding the load
Last edited by Your_mum; Jun 29th, 2009 at 2:59 pm.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 385
Reputation: leegeorg07 is an unknown quantity at this point 
Solved Threads: 30
leegeorg07's Avatar
leegeorg07 leegeorg07 is offline Offline
Posting Whiz

Re: Adress book

 
0
  #10
Jun 29th, 2009
I have to say that from python 2.5 and on you can use the 'a' file open method (append), which just adds to the data already in the file, i changed all of my programs to use this instead and it works so much faster
don't judge me because I'm a year 8!

'it is better to fight for something than to live for nothing'General George S Patton
Reply With Quote Quick reply to this message  
Reply

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



Similar Threads
Other Threads in the Python Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC