| | |
Adress book
![]() |
•
•
Join Date: Jun 2009
Posts: 28
Reputation:
Solved Threads: 0
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:
Please help!!!!!!
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:
Python Syntax (Toggle Plain Text)
class Data: def __init__(self, home, mobile, email): self.home = home self.mobile = mobile self.email = email def Coll_Data(self): self.info = """ Home Phone: %s Mobile Phone: %s Email: %s """ % (self.home, self.mobile, self.email) #the bit that doesn't work that needs changing is below: def add_new(): name = raw_input("Enter name to be added: ") h = raw_input("Enter home phone: ") m = raw_input("Enter Mobile phone: ") e = raw_input("Enter Email address: ") name = Data(h, m, e)
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.
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.
•
•
Join Date: Jun 2009
Posts: 28
Reputation:
Solved Threads: 0
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:
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.
I have rejigged the code:
Python Syntax (Toggle Plain Text)
class Data: def __init__(self): self.home = raw_input("Enter home: ") self.mobile = raw_input("Enter mobile: ") self.email = raw_input("Enter email :") def Coll_data(self): self.info = """ Home : %s Mobile : %s Email : %s """ % (self.home, self.mobile, self.email)
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
This works just fine:
Remember that the name string is the dictionary key and has to be unique.
python Syntax (Toggle Plain Text)
# create a dictionary of class instance values class Data(object): def __init__(self, home, mobile, email): self.home = home self.mobile = mobile self.email = email self.get_info() def get_info(self): sf = \ """Home Phone: %s Mobile Phone: %s Email: %s """ return sf % (self.home, self.mobile, self.email) name_dic = {} # first set of data name = 'frank' home = '123-456-7890' mobile = '456-789-0123' email = 'frank23@gmail.com' name_dic[name] = Data(home, mobile, email) # second set of data name = 'bob' home = '234-567-8901' mobile = '567-690-1234' email = 'bob.dork@hotmail.com' name_dic[name] = Data(home, mobile, email) # get bob's email name = 'bob' print( name_dic[name].email ) print('-'*30) # get the info for all names added so far for key in name_dic: print( "Name = %s" % key ) print( name_dic[key].get_info() ) """ my output --> bob.dork@hotmail.com ------------------------------ Name = frank Home Phone: 123-456-7890 Mobile Phone: 456-789-0123 Email: frank23@gmail.com Name = bob Home Phone: 234-567-8901 Mobile Phone: 567-690-1234 Email: bob.dork@hotmail.com """
Last edited by sneekula; Jun 28th, 2009 at 12:56 pm.
No one died when Clinton lied.
•
•
Join Date: Jun 2009
Posts: 28
Reputation:
Solved Threads: 0
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
so how can I take n_e and swap it for the old email adress
heres the code so far
Python Syntax (Toggle Plain Text)
intro = "Welcome to Adress_Book.py" name_dic = {} loop = 1 menu_choice = 0 class Data(object): def __init__(self, home, mobile, email): self.home = home self.mobile = mobile self.email = email self.get_info() def get_info(self): sf = \ """Home Phone: %s Mobile Phone: %s Email: %s """ return sf % (self.home, self.mobile, self.email) def add_new(): name = raw_input("Enter name: ") home = raw_input("Enter home: ") mobile = raw_input("Enter mobile: ") email = raw_input("Enter email: ") name_dic[name] = Data(home, mobile, email) def printout(): for key in name_dic: print( "Name = %s" % key ) print( name_dic[key].get_info() ) def look_up(): search = raw_input("Enter name to be searched: ") if name_dic.has_key(search): print( "Name = %s" % search ) print( name_dic[search].get_info() ) else: print( "Name %s not found" % search ) def del_entry(): d_search = raw_input("Enter name to be deleted: ") if name_dic.has_key(d_search): del name_dic[d_search] print( "Name %s deleted" % d_search) else: print( "Name %s not found" % d_search ) def change_att(): c_search = raw_input("Enter name to alter: ") if name_dic.has_key(c_search): c_name = raw_input("What element do you want to change: ") if c_name == 'email': n_e = raw_input("Enter new email adress: ") #n_e being the new email adress
•
•
Join Date: Jun 2009
Posts: 28
Reputation:
Solved Threads: 0
sorry my mistake 
I want to write file I/O for this, having tried before and failed, so could someone pls point me in the right direction.

Python Syntax (Toggle Plain Text)
intro = "Welcome to Adress_Book.py" name_dic = {} loop = 1 menu_choice = 0 class Data(object): def __init__(self, home, mobile, email): self.home = home self.mobile = mobile self.email = email self.get_info() def get_info(self): sf = \ """Home Phone: %s Mobile Phone: %s Email: %s """ return sf % (self.home, self.mobile, self.email) def add_new(): name = raw_input("Enter name: ") home = raw_input("Enter home: ") mobile = raw_input("Enter mobile: ") email = raw_input("Enter email: ") name_dic[name] = Data(home, mobile, email) def printout(): for key in name_dic: print( "Name = %s" % key ) print( name_dic[key].get_info() ) def look_up(): search = raw_input("Enter name to be searched: ") if name_dic.has_key(search): print( "Name = %s" % search ) print( name_dic[search].get_info() ) else: print( "Name %s not found" % search ) def del_entry(): d_search = raw_input("Enter name to be deleted: ") if name_dic.has_key(d_search): del name_dic[d_search] print( "Name %s deleted" % d_search) else: print( "Name %s not found" % d_search ) def change_att(): c_search = raw_input("Enter name to alter: ") if name_dic.has_key(c_search): c_name = raw_input("What element do you want to change: ") if c_name == 'email': n_e = raw_input("Enter new email adress: ") name_dic[c_search].email = n_e elif c_name == 'home': n_h = raw_input("Enter new home number: ") name_dic[c_search].home = n_h elif c_name == 'mobile': n_m = raw_input("Enter new mobile number: ") name_dic[c_search].mobile = n_m else: print( "Name %s not found" % c_search ) print intro while loop != 0: print """ 1) Add new entry 2) Remove an entry 3) Look up an entry 4) Print out adress book 5) Edit an entry 6) Exit """ menu_choice = input("Enter choice: ") if menu_choice == 1: add_new() elif menu_choice == 2: del_entry() elif menu_choice == 3: look_up() elif menu_choice == 4: printout() elif menu_choice == 5: change_att() elif menu_choice == 6: loop = 0 else: print "Please anter a valid option"
Last edited by Your_mum; Jun 29th, 2009 at 2:18 pm.
Let's say you want to change the email:
python Syntax (Toggle Plain Text)
# a test setup class Data(object): def __init__(self, home, mobile, email): self.home = home self.mobile = mobile self.email = email self.get_info() def get_info(self): sf = \ """Home Phone: %s Mobile Phone: %s Email: %s """ return sf % (self.home, self.mobile, self.email) name_dic = {} # first set of data for testing name = 'frank millato' home = '123-456-7890' mobile = '456-789-0123' email = 'frank23@gmail.com' name_dic[name] = Data(home, mobile, email) # second set of data for testing name = 'bob dork' home = '234-567-8901' mobile = '567-690-1234' email = 'bob.dork@hotmail.com' name_dic[name] = Data(home, mobile, email) def change_att(): #c_search = raw_input("Enter name to alter: ") c_search = 'bob dork' # for testing only!!! if name_dic.has_key(c_search): #c_name = raw_input("What element do you want to change: ") c_name = 'email' # for tesing only!!! if c_name == 'email': #n_e = raw_input("Enter new email adress: ") n_e = 'bob.dork@yahoo.com' # for testing only!!! # make the change k = name_dic[c_search] name_dic[c_search] = Data(k.home, k.mobile, n_e) # get the info for all names added before change for key in name_dic: print( "Name = %s" % key ) print( name_dic[key].get_info() ) print('-'*40) change_att() # get the info for all names added after change for key in name_dic: print( "Name = %s" % key ) print( name_dic[key].get_info() ) """ my output --> Name = frank millato Home Phone: 123-456-7890 Mobile Phone: 456-789-0123 Email: frank23@gmail.com Name = bob dork Home Phone: 234-567-8901 Mobile Phone: 567-690-1234 Email: bob.dork@hotmail.com ---------------------------------------- Name = frank millato Home Phone: 123-456-7890 Mobile Phone: 456-789-0123 Email: frank23@gmail.com Name = bob dork Home Phone: 234-567-8901 Mobile Phone: 567-690-1234 Email: bob.dork@yahoo.com """
No one died when Clinton lied.
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
'it is better to fight for something than to live for nothing'General George S Patton
![]() |
Similar Threads
- ideas for project.. (C)
- Virtual Address Book Help (Python)
- address book won't open (OS X)
- writitng data from php to mysql (PHP)
- Beginner.........Books (MySQL)
- IE6 has been constantly hijacked by .... (Viruses, Spyware and other Nasties)
- Motorola T720 synching phone book (Cellphones, PDAs and Handheld Devices)
- Book Reviews (Geeks' Lounge)
- Error in Wrox Book (Perl)
Other Threads in the Python Forum
- Previous Thread: wxPython Error
- Next Thread: Help updating images and texts
| Thread Tools | Search this Thread |
abrupt alarm ansi anti approximation array assignment backend beginner binary bluetooth builtin calculator character cmd converter countpasswordentry curved customdialog cx-freeze dan08 data decimals dictionary directory dynamic exe file float format function generator getvalue gnu graphics halp heads homework http ideas input ip itunes java leftmouse line linux list lists loop maze module mouse mysqlquery number numbers output parsing path pointer prime programming progressbar push py2exe pygame python random recursion schedule screensaverloopinactive script scrolledtext slicenotation sqlite ssh statistics string strings sudokusolver terminal text thread time tlapse tuple twoup ubuntu unicode urllib urllib2 variable ventrilo vigenere web webservice wikipedia write wxpython xlib






