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 992 2
Number Guessing Game using Python 683 2
Pyperclip: Not Implemented Error 1033 2
Hangman Game using Python 16821 2
Using Databases with CherryPy application 1676 2
nose: Working 509 2
pytest: Working 512 2
Open Source and Hacktoberfest 868 2
Managing Logs of CherryPy applications 1005 2
Top 20 Data Science Tools 684 2
Ajax application using CherryPy 799 2
REST application using CherryPy 664 2
On Screen Keyboard using Python 5532 2
Elastic Net Regression 816 2
US Presidential Election 2020 Prediction using Python 795 2
Sound Source Separation 1166 2
URLs with Parameters in CherryPy 1635 2
Testing CherryPy application 638 2
Handling HTML Forms with CherryPy 1450 2
Applications of Natural Language Processing in Businesses 511 2
NetworkX: Multigraphs 649 2
Tracking User Activity with CherryPy 1404 2
CherryPy: Handling Cookies 822 2
Introduction to NetworkX 633 2
TorchServe - Serving PyTorch Models 1306 2
Fake News Detection Model using Python 735 2
Keeping Home Routers secure while working remotely 484 2
Email Slicer using Python 2998 2
NetworkX: Creating a Graph 1111 2
Best Mathematics Courses for Machine Learning 551 2
Hello World in CherryPy 681 2
Building dependencies as Meson subprojects 979 2
Vehicle Detection System 1081 2
NetworkX: Examining and Removing Graph Elements 608 2
Handling URLs with CherryPy 537 2
PEP 8 - Guide to Beautiful Python Code 759 2
NetworkX: Drawing Graphs 624 2
Mad Libs Game using Python 645 2
Hosting Cherry applications 613 2
Top 5 Free Online IDEs of 2020 868 2
pytest: Introduction 535 2
Preventing Pwned and Reused Passwords 582 2
Contact Book using Python 2096 2
Introduction to CherryPy 547 2
nose: Introduction 505 2
Text-based Adventure Game using Python 3002 2
NetworkX: Adding Attributes 2290 2
NetworkX: Directed Graphs 1022 2
Dice Simulator using Python 562 2
Decorating CherryPy applications using CSS 834 2

Comments