Question

Using arrays create a personal phone directory that contains room for first names and phone numbers...

Using arrays create a personal phone directory that contains room for first names and phone numbers for 20 people. Using the "names.txt" retrieve the first name and phone number for each person and store in arrays (parallel).

Prompt the user for a name, search for the name, and if name is found in the array, display the corresponding phone number. If the name is not found, prompt the user to enter a phone number, and add the new name and phone number to the list. Continue to prompt the user for names until the user enters quit or until arrays are full. After the arrays are full (containing 20 names), do not allow the user to add new entries. Print the full list.

"names.txt"

Denton (123)-456-7890

Francis (123)-456-7891

Lucy (123)-456-7892

Brooklyn (123)-456-7899

Mattie (123)-456-7898

Clarissa (123)-456-7897

Bailey (123)-456-7896

Lesley (123)-456-7895

Tristan (123)-456-7894

Bethany (123)-456-7893

0 0
Add a comment Improve this question Transcribed image text
Answer #1
# enter path the file name replace student.txt
text_file = open("student.txt", "r");
# read text file and save it in array lines
lines = text_file.readlines();
findname=" ";
# create two arrays to store names ans phone numbers fro text file
# size of both list will be 20 where they have None
# ex: [None,None,------,None]
names=[None] * 20;
phoneNo=[None] * 20;
# using for loop we will store names and phone number in arrays
index=0;
for x in lines:
    temp= x.split(' ');
    names[index]=temp[0];
    phoneNo[index]=temp[1];
    index=index+1;
# this function check weather name is present in list or not
def checkName(name):
    if name in names:
        return names.index(name);
    else:
        return 999;
# enter the name for the first time
findname=input("Find a name: ");
# this while loop exists when user type quit
while(findname != 'quit'):
#if 20th position of names is filled with name, while loop will exit
  if(names[len(names)-1]!=None):
    break;
  else:
    output=checkName(findname);
# if return result is not 999 it means name is found
    if output!=999:
      print("Found the name");
      print(phoneNo[output]);
      findname="quit";
      
# if 999 us returned it means name is not found and you have to enter
# name and phone number of that person
    else:
      print("Not found the name!",findname);
      enterphoneno= input("enter phone no for:");
# here we are storing the name and phone no of that person
      names[index]=findname;
      phoneNo[index]=enterphoneno;
      if(names[len(names)-1]!=None):
        break;
      findname=input("Find a name: ");
      index=index+1;

# print all the names and corrosponding phone number of the persons

for x in range(0,19):
  if(names[x]!= None):
    print(names[x],":",phoneNo[x]);
Add a comment
Know the answer?
Add Answer to:
Using arrays create a personal phone directory that contains room for first names and phone numbers...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT