Telephone Directory
Telephone Directory
import sys
# this function will be the first to run as soon as the main function executes
def initial_phonebook():
# We are collecting the initial number of contacts the user wants to have in the
# phonebook already. User may also enter 0 if he doesn't wish to enter any.
phone_book = []
print(phone_book)
for i in range(rows):
print("....................................................................")
temp = []
for j in range(cols):
# We have taken the conditions for values of j only for the personalized fields
if j == 0:
# We need to check if the user has left the name empty as its
mentioned that
sys.exit(
# takes care of it. Int value cannot accept a blank as that counts as a
string.
if j == 2:
# Even if this field is left as blank, None will take the blank's place
temp[j] = None
if j == 3:
# Only while searching the user will have to enter query exactly the
same way as
# Even if this field is left as blank, None will take the blank's place
temp[j] = None
if j == 4:
temp.append(
# Even if this field is left as blank, None will take the blank's place
temp[j] = None
phone_book.append(temp)
print(phone_book)
return phone_book
def menu():
print("********************************************************************")
print("********************************************************************")
# Out of the provided 6 choices, user needs to enter any 1 choice among the 6
# We return the entered choice to the calling function wiz main in our case
return choice
def add_contact(pb):
dip = []
for i in range(len(pb[0])):
if i == 0:
if i == 1:
if i == 2:
if i == 3:
if i == 4:
dip.append(
pb.append(dip)
# And once you modify the list, you return it to the calling function wiz main, here.
return pb
def remove_existing(pb):
query = str(
input("Please enter the name of the contact you wish to remove: "))
# We'll collect name of the contact and search if it exists in our phonebook
temp = 0
for i in range(len(pb)):
if query == pb[i][0]:
temp += 1
return pb
if temp == 0:
# Now if at all any case matches temp should've incremented but if otherwise,
# temp will remain 0 and that means the query does not exist in this phonebook
return pb
def delete_all(pb):
# This function will simply delete all the entries in the phonebook pb
return pb.clear()
def search_existing(pb):
# This function searches for an existing contact and displays the result
# We're doing so just to ensure that the user experiences a customized search result
temp = []
check = -1
if choice == 1:
query = str(
input("Please enter the name of the contact you wish to search: "))
for i in range(len(pb)):
if query == pb[i][0]:
check = i
temp.append(pb[i])
elif choice == 2:
query = int(
input("Please enter the number of the contact you wish to search: "))
for i in range(len(pb)):
if query == pb[i][1]:
check = i
temp.append(pb[i])
elif choice == 3:
for i in range(len(pb)):
if query == pb[i][2]:
check = i
temp.append(pb[i])
elif choice == 4:
for i in range(len(pb)):
if query == pb[i][3]:
check = i
temp.append(pb[i])
elif choice == 5:
query = str(
input("Please enter the category of the contact you wish to search: "))
for i in range(len(pb)):
if query == pb[i][4]:
check = i
temp.append(pb[i])
# All contacts under query category will be shown using this feature
else:
# If the user enters any other choice then the search will be unsuccessful
return -1
# all the searches are stored in temp and all the results will be displayed with
if check == -1:
return -1
# returning -1 indicates that the query did not exist in the directory
else:
display_all(temp)
return check
# we're just returning a index value wiz not -1 to calling function just to notify
def display_all(pb):
if not pb:
# if display function is called after deleting all contacts then the len will be 0
else:
for i in range(len(pb)):
print(pb[i])
def thanks():
print("********************************************************************")
print("********************************************************************")
print("....................................................................")
print("....................................................................")
# You're free to modify your interface as per your will to make it look interactive