Phonebook Program Revamped — #LearnPythonThroughProjects : Series 11

Aminah Mardiyyah Rufai
7 min readSep 23, 2020

In the previous series, ‘a simple phonebook program’ was built using basic concepts of Functions, data structures, and flow control statements. Taking a step ahead, an improved version will be built today, working towards automation and fewer yet efficient lines of codes. We will apply the knowledge of user-defined modules once again, to get more comfortable with building one.

Excited about this one? Me too. Watch out for an important announcement at the end of the article.

Let’s move on to building the Project for the day.

Source: Here

PROJECT PUEDOCODE

  1. PYCONTACT MODULE
  • Define an empty dictionary to store contact information as a global variable.
  • Define a function ‘menu’ to welcome the user and provide options on how the phonebook works.
  • Use a try-except block inside the function to take care of possible wrong responses by the user.
    i. Define a variable ‘entry’ for user options inside the ‘try’ block.
  • For the except block, use a ‘valueerror’ exception
  • In the exception block re-define the variable ‘entry’, letting the users aware of what type of inputs should be in there.
  • Include decision-making processes using the control flow statements. This flow control statement will call other functions defined beneath

See the image below for the first six(6) steps

Moving on to further steps:

Do not forget to take note of proper indentation and syntax. Remember that Python is both space and case-sensitive.

  • Define another function ‘display query’; with required argument query, as ‘disp(query)’.
  • Set up a decision-making process using control flow statements
  • Initiate two new functions ‘DisplayAll’ and ‘Addcontact’, to be defined below.
  • Define a function ‘DisplayAll’
  • Include decision making using flow control statements.
  • Also, initiate the ‘AddContact’ function here.
  • Call the ‘menu’ function defined earlier at the end of this function.
  • Define a function ‘AddContact’.
  • Use the try-except block here also.
  • Include conditions for decision-making.
  • Call the ‘DisplayAll’ function.
  • Call the ‘disp(query)’ function here, as such ‘ disp(name)’.
  • Define a new function ‘ViewContact’.
  • Using decision-making here also.
  • Call the ‘disp(query)’ function here too.
  • Final function for the module. Define a function ‘DelContact’ using a positional argument.
  • Initiate a decision making.
  • Call the ‘DisplayAll’ function.

Save this entire file as ‘pycontact_module.py’

Moving on to the second part of the program.

2. PHONEBOOK PROGRAM RUN

Open a new file and save it as ‘phonebook_revamped.py’.

  • Import the ‘pycontact_module’ contact using the * attribute for all functions.

PROGRAM RUN:

It’s a mindblowing project, don’t you agree?

PROGRAM EXPLANATION

Let’s break down the working principle. So a module was created, where multiple functions were defined and called, an empty dictionary was also initiated as a ‘Global variable. So what is a ‘Global variable’?

Generally, in programming, there are two types of variables defined in a function or ‘Block of codes’. These are called ‘Local and Global variables’.

While Global variables are variables defined outside the block of codes/functions and can be called and used anywhere else in the program. Just as the name ‘Global’, they are not specific to a particular function or code. Local Variables apply only to a particular block of code/function and can only be used therein. Examples of such in this program are: ‘name, contact, query, confirm’.

Source: Here

The first function created was the ‘menu’ function’. Very similar to what was done in the previous series, a menu was initiated with options provided for the user to select what operation should be carried out. Here comes the difference. A new Python concept called ‘the try-except’ block was applied here. This was done to take care of possible wrong entries by the user and to avoid errors that could have come up as a result. This concept is called ‘Exception-Handling’ in Python.

An exception is an occurrence, otherwise referred to as an error that disrupts the flow of a program. Exception Handling is the process used to escape and treats these likely ‘bugs’(no one likes bugs in their codes). So how does it work? Very easy, a programmer simply thinks of possible cases of errors that could occur while the program is executed, taking into consideration common human errors, and creating a decision for what should or should not happen when these errors are encountered. That’s right! A try-except block is a simple decision-making process, where actions for what should occur are placed. This helps to avoid sudden disrupt of a program and helps to avoid those little annoying code bugs. There are several types of errors though, a ‘ValueError’ was used in this case, because that is the possible error that would have occurred should a user enter a number in place of a string or vice versa(Explore by running the program with this block and notice what happens). Also, do refer to the links below for a better understanding on this.

So several conditions were also initiated using conditional statements, keeping in mind possible selected options by the user. In this block, notice some additional functions were called. This was used to avoid repetition. Recall the benefits of using functions? D.R.Y

For the rest of the functions:

  1. disp(query)’: For Displaying exiting contacts in the phonebook and if the contact does not exist in the phonebook, the ‘DisplayAll()’ function is called.
  2. DisplayAll()’: In this function, conditions were set for in such a way that if the contact book is empty, a prompt for adding contacts is displayed, otherwise existing contacts are printed.
  3. AddContact()’: In this function, Again the try-except block was used to escape possible wrong entry by the user, and decisions created for adding a new contact or detecting if the entered contact already exists.
  4. ViewContact()’: A simple function for initiating a view option by the user, for a specific contact. Notice the ‘disp(query)’ function was also called here to take care of none exiting contacts and delete option by the user.
  5. DelContact()’: The final function for the module, which takes care of delete options.

Finally, in the ‘Phonebook.py’ file, the module was imported, using the *attribute which imports all variables, functions, or classes stored in a module(Do you remember the different ways of importing and using a module?). A simple function was created which simply pulls out every other function form the module and gets the program running. Amazing!!

Wow! Do you know you are gradually taking off the beginners/newbie python programmer jacket and stepping into a pro-programmer?

Great Job!

Thank you for tuning in once again!

If you are yet to see the previous ‘phonebook’ program, see the link below. Also, check out the previous series.

IMPORTANT ANNOUNCEMENT :

I am happy to announce that someone will be collaborating and joining the series: Abubakar Ateeq. All credits go to him also for this series. All codes used in this article can be found in the repository below:

Find useful links below! See you in the next series.

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