Contact Book using Python














































Contact Book using Python



Contact Book using Python

In this article, we will make a contact book. For this we will create a class containing our contacts as lists. We will use two lists so that we can search by name and phone number both. Our class will contain different methods for different processes we will use.


At the end we will create a Command Line Interface for different operations to perform in the contact book.


Save the below code in a file named 'contact.py'.


class Contact: def __init__(self): self.book = [] self.names = [] self.numbers = [] def create(self, name, number, email): self.book.append([name, number, email]) self.names.append(name) self.numbers.append(number) print(name, 'Contact created successfully..!') def update(self, name=False, number=False): # -------------------------------- Update Using Names ----------------------# if name: if name in self.names: inames = [i for i, x in enumerate(self.names) if x == name] print('No.of contacts found: ', len(inames), inames) c = 1 for iname in inames: print(c, 'name: ', self.names[iname], 'details: ', self.book[iname], '\n') c += 1 d = 'y' while d: inp = input( "Press 'y' to continue or exit to 'n': ").lower() if inp == 'y': nm_i = int( input('Enter index of listed contact above: ')) try: data = inames[nm_i-1] print('\nDetails: ', self.book[data]) print(1, '- Name: ', self.book[data][0]) print(2, '- Number: ', self.book[data][1]) print(3, '- Email: ', self.book[data][2]) print(0, '- Exit') v = 'y' while v: ch = int( input('Enter index of listed contact above: ')) if ch == 1: ch_name = input('Enter name to update: ') self.book[data][0] = ch_name self.names[data] = ch_name elif ch == 2: ch_number = input( 'Enter Number to update: ') self.book[data][1] = ch_number self.numbers[data] = ch_number elif ch == 3: ch_email = input('Enter Email to update: ') self.book[data][2] = ch_email elif ch == 0: v = False else: v = False print('#---------------- Updated contact -------------#') print(self.book[data]) print(self.names[data]) print(self.numbers[data]) except: print('Index not found..!') else: break else: print('No contact found..!') # -------------------------------- Update Using Numbers ----------------------# elif number: if number in self.numbers: inumbers = [i for i, x in enumerate( self.numbers) if x == number] print('No.of contacts found: ', len(inumbers)) c = 1 for inumber in inumbers: print( c, 'Number: ', self.numbers[inumber], 'details: ', self.book[inumber], '\n') c += 1 d = 'y' while d: inp = input("Press 'y' to continue or exit to 'n': ") if inp == 'y': nm_i = int( input('Enter index of listed contact above: ')) try: data = inumbers[nm_i-1] print('\nDetails: ', self.book[data]) print(1, '- Name: ', self.book[data][0]) print(2, '- Number: ', self.book[data][1]) print(3, '- Email: ', self.book[data][2]) print(0, '- Exit') v = 'y' while v: ch = int( input('Enter index of listed contact above: ')) if ch == 1: ch_name = input('Enter name to update: ') self.book[data][0] = ch_name self.names[data] = ch_name elif ch == 2: ch_number = input( 'Enter Number to update: ') self.book[data][1] = ch_number self.numbers[data] = ch_number elif ch == 3: ch_email = input('Enter Email to update: ') self.book[data][2] = ch_email elif ch == 0: v = False else: v = False print('#---------------- Updated contact -------------#') print(self.book[data]) print(self.names[data]) print(self.numbers[data]) except: print('Index not found..!') else: break else: print('No contact found..!') else: print('No conatact Found..!') #------------------------------ DELETE Contacts --------------------------# def delete(self, name=False, number=False): if name: if name in self.names: d = 'y' while d: inames = [i for i, x in enumerate(self.names) if x == name] if len(inames) > 0: print('No.of contacts found: ', len(inames), inames) c = 1 for iname in inames: print( c, 'name: ', self.names[iname], 'details: ', self.book[iname], '\n') c += 1 inp = input("Press 'y' to continue or exit to 'n': ") if inp == 'y': nm_i = int( input('Enter index of listed CONTACT above: ')) try: data = inames[nm_i-1] print('\nDetails: ', self.book[data]) f = input( 'Are you sure want to DELETE..? (y/n): ').lower() if f == 'y': del self.book[data] del self.numbers[data] del self.names[data] print( '#---------------- Updated contact Book -------------#') print('\nDetails: ', self.book) print('\nName Entries: ', self.names) print('\nNumber Entries: ', self.numbers) except: print('\nIndex not found..!') else: break else: print('0 contacts found..!') break else: print('Contact not found..!') elif number: if number in self.numbers: d = 'y' while d: inumbers = [i for i, x in enumerate( self.numbers) if x == number] if len(inumbers) > 0: c = 1 for inumber in inumbers: print( c, 'number: ', self.numbers[inumber], 'details: ', self.book[inumber], '\n') c += 1 inp = input("Press 'y' to continue or exit to 'n': ") if inp == 'y': nm_i = int( input('Enter index of listed CONTACT above: ')) try: data = inumbers[nm_i-1] print('\nDetails: ', self.book[data]) f = input( 'Are you sure want to DELETE..? (y/n): ').lower() if f == 'y': del self.book[data] del self.numbers[data] del self.names[data] print( '#---------------- Updated contact Book -------------#') print('\nDetails: ', self.book) print('\nName Entries: ', self.names) print('\nNumber Entries: ', self.numbers) except: print('\nIndex not found..!') else: break else: print('0 contacts found..!') break else: print('No contact Found..!') else: print('No conatact Found..!') #------------------------- PhoneBook ---------------------------# def phonebook(self): print('\nTotal Contacts found: ', len(self.book)) for contact in self.book: print('\nName: ', contact[0]) print('Phone Number: ', contact[1]) print('Email: ', contact[2]) #-------------------------------------- Search Contact -----------------------------# def search(self, number=False, name=False): if name: if name in self.names: inames = [i for i, x in enumerate(self.names) if x == name] c = 1 for iname in inames: print(c, 'name: ', self.names[iname], 'details: ', self.book[iname], '\n') c += 1 else: print('No contact found..!') elif number: if number in self.numbers: inumbers = [i for i, x in enumerate( self.numbers) if x == number] c = 1 for inumber in inumbers: print( c, 'number: ', self.numbers[inumber], 'details: ', self.book[inumber], '\n') c += 1 else: print('No contact found..!') else: print('No contact found..!') menu = ''' 1.Create 2.Update 3.List Contacts 4.Search 5.Delete 6.Exit ''' if __name__ == "__main__": c = Contact() a = True while a: print('\n', menu, '\n') choice = int(input('\nEnter your Choice: ')) if choice == 1: name = input('Name: ') phone = input('Phone No.: ') email = input('E-mail Address: ') c.create(name, phone, email) elif choice == 2: k = True while k: print( ''' 1. Update through name 2. Update through number 3. Exit ''' ) inp = int(input('Enter input: ')) if inp == 1: name = input('Enter name: ') c.update(name=name) elif inp == 2: number = input('Enter Number: ') c.update(number=number) else: k = False elif choice == 3: c.phonebook() elif choice == 4: k = True while k: print( ''' 1. Search through name 2. Search through number 3. Exit ''' ) inp = int(input('Enter input: ')) if inp == 1: name = input('Enter name: ') c.search(name=name) elif inp == 2: number = input('Enter Number: ') c.search(number=number) else: k = False elif choice == 5: k = True while k: print( ''' 1. Delete through name 2. Delete through number 3. Exit ''' ) inp = int(input('Enter input: ')) if inp == 1: name = input('Enter name: ') c.delete(name=name) elif inp == 2: number = input('Enter Number: ') c.delete(number=number) else: k = False elif choice == 6: a = False else: break


We have created a menu with 6 options and we have methods in our class to perform each of these operations (except exit). The code is big because of the different operations we can perform but it is quite simple and intuitive.


To run the program, enter the following command in a Terminal/Command Line


python3 contact.py


A sample output will look like this


 

          1.Create

          2.Update

          3.List Contacts

          4.Search

          5.Delete

          6.Exit

 



Enter your Choice: 1

Name: Aniket

Phone No.: 9999123456

E-mail Address: email@example.com

Aniket Contact created successfully..!


 

          1.Create

          2.Update

          3.List Contacts

          4.Search

          5.Delete

          6.Exit

 



Enter your Choice: 3


Total Contacts found:  1


Name:  Aniket

Phone Number:  9999123456

Email:  email@example.com


 

          1.Create

          2.Update

          3.List Contacts

          4.Search

          5.Delete

          6.Exit

 



Enter your Choice: 2


                    1. Update through name

                    2. Update through number

                    3. Exit

                    

Enter input: 1

Enter name: Aniket

No.of contacts found:  1 [0]

1 name:  Aniket details:  ['Aniket', '9999123456', 'email@example.com'] 


Press 'y' to continue or exit to 'n': y

Enter index of listed contact above: 1


Details:  ['Aniket', '9999123456', 'email@example.com']

1 - Name:  Aniket

2 - Number:  9999123456

3 - Email:  email@example.com

0 - Exit

Enter index of listed contact above: 2

Enter Number to update: 9999912345

Enter index of listed contact above: 0

#---------------- Updated contact -------------#

['Aniket', '9999912345', 'email@example.com']

Aniket

9999912345

Press 'y' to continue or exit to 'n': n


                    1. Update through name

                    2. Update through number

                    3. Exit

                    

Enter input: 2

Enter Number: 9999912345

No.of contacts found:  1

1 Number:  9999912345 details:  ['Aniket', '9999912345', 'email@example.com'] 


Press 'y' to continue or exit to 'n': y

Enter index of listed contact above: 1


Details:  ['Aniket', '9999912345', 'email@example.com']

1 - Name:  Aniket

2 - Number:  9999912345

3 - Email:  email@example.com

0 - Exit

Enter index of listed contact above: 1

Enter name to update: Aniket Sharma

Enter index of listed contact above: 0

#---------------- Updated contact -------------#

['Aniket Sharma', '9999912345', 'email@example.com']

Aniket Sharma

9999912345

Press 'y' to continue or exit to 'n': n


                    1. Update through name

                    2. Update through number

                    3. Exit

                    

Enter input: 3


 

          1.Create

          2.Update

          3.List Contacts

          4.Search

          5.Delete

          6.Exit

 



Enter your Choice: 3


Total Contacts found:  1


Name:  Aniket Sharma

Phone Number:  9999912345

Email:  email@example.com


 

          1.Create

          2.Update

          3.List Contacts

          4.Search

          5.Delete

          6.Exit

 



Enter your Choice: 4


                    1. Search through name

                    2. Search through number

                    3. Exit

                    

Enter input: 1

Enter name: Aniket Sharma

1 name:  Aniket Sharma details:  ['Aniket Sharma', '9999912345', 'email@example.com'] 



                    1. Search through name

                    2. Search through number

                    3. Exit

                    

Enter input: 2

Enter Number: 9999912345

1 number:  9999912345 details:  ['Aniket Sharma', '9999912345', 'email@example.com'] 



                    1. Search through name

                    2. Search through number

                    3. Exit

                    

Enter input: 3


 

          1.Create

          2.Update

          3.List Contacts

          4.Search

          5.Delete

          6.Exit

 



Enter your Choice: 5


                    1. Delete through name

                    2. Delete through number

                    3. Exit

                    

Enter input: 1

Enter name: Aniket Sharma

No.of contacts found:  1 [0]

1 name:  Aniket Sharma details:  ['Aniket Sharma', '9999912345', 'email@example.com'] 


Press 'y' to continue or exit to 'n': y

Enter index of listed CONTACT above: 1


Details:  ['Aniket Sharma', '9999912345', 'email@example.com']

Are you sure want to DELETE..? (y/n): y

#---------------- Updated contact Book -------------#


Details:  []


Name Entries:  []


Number Entries:  []

0 contacts found..!


                    1. Delete through name

                    2. Delete through number

                    3. Exit

                    

Enter input: 3


 

          1.Create

          2.Update

          3.List Contacts

          4.Search

          5.Delete

          6.Exit

 



Enter your Choice: 3


Total Contacts found:  0


 

          1.Create

          2.Update

          3.List Contacts

          4.Search

          5.Delete

          6.Exit

 



Enter your Choice: 6



More Articles of Aniket Sharma:

Name Views Likes
Pyperclip: Installation and Working 1131 2
Number Guessing Game using Python 768 2
Pyperclip: Not Implemented Error 1546 2
Hangman Game using Python 20008 2
Using Databases with CherryPy application 2273 2
nose: Working 601 2
pytest: Working 600 2
Open Source and Hacktoberfest 981 2
Managing Logs of CherryPy applications 1177 2
Top 20 Data Science Tools 787 2
Ajax application using CherryPy 930 2
REST application using CherryPy 777 2
On Screen Keyboard using Python 8409 2
Elastic Net Regression 1008 2
US Presidential Election 2020 Prediction using Python 877 2
Sound Source Separation 1376 2
URLs with Parameters in CherryPy 2267 2
Testing CherryPy application 766 2
Handling HTML Forms with CherryPy 1908 2
Applications of Natural Language Processing in Businesses 605 2
NetworkX: Multigraphs 876 2
Tracking User Activity with CherryPy 1664 2
CherryPy: Handling Cookies 1201 2
Introduction to NetworkX 715 2
TorchServe - Serving PyTorch Models 1539 2
Fake News Detection Model using Python 839 2
Keeping Home Routers secure while working remotely 567 2
Email Slicer using Python 3167 2
NetworkX: Creating a Graph 1207 2
Best Mathematics Courses for Machine Learning 644 2
Hello World in CherryPy 960 2
Building dependencies as Meson subprojects 1235 2
Vehicle Detection System 1223 2
NetworkX: Examining and Removing Graph Elements 704 2
Handling URLs with CherryPy 626 2
PEP 8 - Guide to Beautiful Python Code 860 2
NetworkX: Drawing Graphs 739 2
Mad Libs Game using Python 757 2
Hosting Cherry applications 715 2
Top 5 Free Online IDEs of 2020 993 2
pytest: Introduction 612 2
Preventing Pwned and Reused Passwords 658 2
Contact Book using Python 2331 2
Introduction to CherryPy 636 2
nose: Introduction 574 2
Text-based Adventure Game using Python 3487 2
NetworkX: Adding Attributes 3242 2
NetworkX: Directed Graphs 1245 2
Dice Simulator using Python 659 2
Decorating CherryPy applications using CSS 1034 2

Comments