Greetings to all;
Welcome to my daily blog. In todays episode of programming tutorials with me, i thought of using python to develop something like a contact book. Where a user can add a name, that will contain details like his phone number and email address. Also the user can search through the contact book to see if an already existing name is present. He can also delete a contact if he wishes.
It may sound complicated but trust me it isn鈥檛. To begin our code, we first need to create a funtion where the contacts would be stored. We can do that by Contacts = {}.
Now that we have gotten this out of the way, we can begin our coding. I will be using the While True function. This function in the code runs until the break or exit() funtion in the code executes.
Under this function, i have created five fuctions you can see from the screenshot above. These functions include, the first which is the welcome message, the second function represented with a 1 is for adding a contact, the second function 2, is used for viewing a contact, the third fucntion 3, Deleting a contact and finally the fourth function 4, which is the exit functions.
So what this menas is that a user has three options, either enter 1,2,3 or 4 to exit. Now we need to create drop down option for each option such that when a user selects each number something happens. So for the first number 1, we will use the If clause
After which we will create functions to allow the user enter his name, number and his email addresses. If you want to you can add more details to it. But for now will stick with this one. There is one more detail we need to add, that is to print that the name has been added. We can use print (f" {name] added to contacts")
If a user selects the second option which is 2, this is where the Else if statement comes in or elif for short. With the input fuction we will ask the user to enter the name we want to search for and then use the if funtion check if the name exits.
If name in contacts the above function will print his name, phone and email. I added the else statement with will print out name not found if the name is not present.
If a user selects the third option which is 3, Again, will use the Else if statement or elif for short. With the input fuction we will ask the user to enter the name we want to delete and then use the if funtion check and delete the name.
If name in contacts, the above function del contacts [name]will delete that contact and then print name deleted from contacts. Im using the else function here again to print name not found if the name cant be found in option 1.
Now for the final piece of the code which is option 4, using print ("Exiting contact book") this will tell the user the code is closing and putting break at the of the code ends the code entirely.
If the user enters 5 or any other number, the else statement at the end of the code will be executed and the user would be told he has entered an invalid choice.
Now the moment we have been waiting for. Let try out the codes. When i run the code, i entered the first option as 1, after which I entered a random phone number, i also entered my email in the promt that was asked.
The output we have;
You can see that the print out says that jimah added to contacts
I tried the second option which is 2. I tried to search for the name i just recently stored which is jimah. For the output we have;
You can see that the print out displays the name, number and address associated to the name
Trying out the third option (delete option). Here will try to delete the contact name jimah. For the output we have;
The fourth and final option (exit) this is meant to end the entire code. Lets check out the output;
You can see that the print out says that code finished
Well thats all for now. You can customize the infotto suite your taste. I would love to hear your thoughts about this, if you have any questions i would love to hear them. Untill then you see you on the next one.