“Building a Simple PhoneBook” — #LearnPythonThroughProjects: Series 10

Aminah Mardiyyah Rufai
7 min readSep 16, 2020

Hey there Python rockstar! Welcome to another interesting Project. Do you have your Coding gear ready? Let’s go build a mind-blowing project.

Image Source Here

In this article, we will explore how to build a simple phonebook in Python, using a blend of all the concepts learnt so far. Are you excited? 😃Me too!😊

Wouldn’t you agree that it’s a pretty good idea to automate and explore the little daily tasks/routines around, using your programming skills?

In the previous article, we scouted the use of user-defined modules and functions in Python .

So let’s probe further, get comfortable with these concepts, revise previously learnt concepts, and build another awesome project.

A summary of the concepts to be used are:

  1. Multiple line strings format
  2. Input and print Functions
  3. Data Structures: Dictionary and dictionary methods ( how to create an empty dictionary, delete and update an item)
  4. User-defined Functions:
  5. Control Flow statements(Iterative, Conditional and Transfer statement(break,return)

PROJECT PSEUDCODE

  1. Define a function to welcome the user and provide options on how the phonebook works. (As follows):

i. Define a function name(‘welcome’)

ii. Create a variable using the input function and multi-line strings format inside the function.

Tips: You can decide to make this function a module and import it. You could also decide to make it a personalized welcome message)

iii. Close the function.

(See image below:)

Welcome function/module

2. Define the main function “phonebook”

i. Initiate a function name ‘phonebook()’.

ii. initiate an empty ‘contact’ dictionary to store values, inside the phonebook function block.

Phonebook function

3. Initiate a loop inside the function to continuously run the phonebook program. (Take note of indentations, this should be inside the phonebook function, and since it is a flow statement, it is also a block of code, which would have its own indentation for codes below it.)

4. Call ‘welcome’ function. Set the entry variable to welcome function

Code so far

5. Create conditions for decision making ,for any option entered by the user.

i. For the first entry Check if the contact dictionary is initially empty. If it is not empty, Print the current contact list in the phonebook.

ii. Otherwise, inform the user that the Phonebook is empty.

Code thus far

6. Create decision making for the second entry.

i. Request for a phone number and contact name

ii. Check if the contact number already exists in the Phonebook

iii. If it does not, update the current Phonebook.

ii. Print a success message.

iii. Loop through Phonebook and display each contact in a separate line

iv. Otherwise, display a message to inform the user.

7. Create a decision for the third entry

i. initiate a ‘name’ variable and ask the user for contact to view.

ii. Create a condition to check if the ‘entered name’ is in the phonebook.

iii. If yes, print the required contact.

iv. Else, inform the user that contact does not exist.

Decision block for third entry

8. Create a decision for the fourth entry

i. Initiate a ‘name’ variable

ii. Repeat steps 7(i-iv)

iii. Confirm if the user wishes to delete the contact.

Decision Block for Fourth Entry

9. Create a decision for the fifth entry. Break the program and exist.

10. If entered entry not in the ‘entry’ variable, display an error message.

Program complete. Phew!!

Save and run the program. (phonebook.py)

Call the function ‘phonebook()’:

Test 1
test 2
Test 3

Great Job! 🎉🎊

So we have already been exposed to some of these concepts already. (If you have not seen the previous articles, use this link). I will move on to explaining the logic behind the code and explain new concepts alongside.

CODE EXPLANATION

So first we created a function called ‘welcome’. As mentioned earlier, you could have also created this as a module, store some ‘frequently used variables in the program’ in there, and just import the module, calling out functions or variables wherever needed (Do explore this option)(Also see this article, on how to create and work user-defined modules.)

A variable called ‘ entry’ was stored in the function ‘welcome’.

The main function ‘phonebook’ was initiated. In this block of code, an empty dictionary was initiated using the default python dictionary representation (‘{}’), this was done so as to be able to store contact names and phone numbers in the form of a dictionary for easy mapping (Remember how a dictionary works with keys and values? See this article: Link).

An Infinite loop was initiated, still inside the ‘phonebook’ function block. The ‘welcome’ function created earlier was called by storing in a variable name ‘entry’.

Conditions were created, using conditional statements, for making decisions for any entry chosen by the user.

For the first entry( entry == 1), which says: ‘Display your Exiting contact’, a decision was set such that if the initial phonebook is empty, i.e no contact has been stored before, it returns a message to inform the user. This was done using the bool() function in python(find more information about this function by looking up the links below). If the value from the ‘bool’ function is not false, the program loops through the phonebook and displays each of the contact information a separate line ( ‘Wondering why a loop and not a print statement was used? So try it out for yourself, find out how each works, and drop a comment in the article on your opinion. I will be delighted to hear from you’). If otherwise, an empty phonebook message is displayed.

code input
code output

For entry == 2(‘Create a New Contact’), two variables(Phone number and Contact name) were initiated. A decision block was also created to check, using one of the variables, if the entry already exists, if not, the phonebook is updated using the Python dictionary ‘update method’, which is one of the methods used to add new items to a Python dictionary. This was done mapping the contact name to the phone number. A success message and the dictionary loop is initiated. However, If the entry does exist, The user is informed.

To get a better understanding of how to work with Python dictionaries(Add, delete, update), Do check out the links below and explore further.

Code block
The output from Code block for entry 2

For entry == 3(‘Check an entry’), a ‘name’ variable was initiated, prompting the user for the contact entry required. Again, decisions were created to check if the entered response exists in the phonebook. If it does, the contact and phone number are displayed, otherwise, an error message is printed to inform the user.

Code block for entry 3
Output from code block 3

For entry 4, very similar to entry 3, with a difference of including the dictionary delete method, after confirmation that the user truly wants the contact deleted. The dictionary method ‘.pop()’ is used.

Code block for Entry 4
Code output for entry 4

Finally, for entry 5, the broke is exited using the break statement, which breaks the infinite while loop initiated at the beginning. Also, an instance for if the user enters a wrong entry was initiated using a final else statement.

Final block

I hope you had fun coding along with me once again. Do explore further. Look up the links below and feel free to tweak and tune as desired. Also, find the link to the Github repository below.

Thank you for reading!

CONNECT ON SOCIAL MEDIA

Twitter: @diyyah92

LinkedIn: https://www.linkedin.com/in/aminah-mardiyyah-rufai/

--

--

Aminah Mardiyyah Rufai

Machine Learning Researcher | Machine Intelligence student at African Institute for Mathematical Sciences and Machine Intelligence | PHD Candidate