521,101 views
7 votes
7 votes
8.19 LAB: Contact list

A contact list is a place where you can store a specific contact with other associated information such as a phone number, email address, birthday, etc. Write a program that first takes in word pairs that consist of a name and a phone number (both strings), separated by a comma. That list is followed by a name, and your program should output the phone number associated with that name. Assume the search name is always in the list.
Ex: If the input is:
Joe,123-5432 Linda,983-4123 Frank,867-5309
Frank
the output is:
867-5309

User Resul
by
2.7k points

1 Answer

25 votes
25 votes

Final answer:

To manage a contact list in a program, split name and phone number pairs, store them in a dictionary, and then retrieve the phone number by searching the name in the dictionary.

Step-by-step explanation:

To write a program that manages a contact list, you will need to store pairs of names and phone numbers, and provide a way to retrieve a phone number by name. First, the program should collect input where each entry consists of a name and a phone number, separated by a comma. Then the program should accept a name and return the associated phone number. Here is a step-by-step guide illustrating the concept:

  • Collect contact entries from the user.
  • Split each entry on the comma to separate the name and phone number.
  • Store the split results in a dictionary with names as keys and phone numbers as values.
  • Accept a name from the user to search the contact list.
  • Output the phone number that corresponds to the given name.

An example of using this program would be if the input is 'Joe,123-5432 Linda,983-4123 Frank,867-5309' followed by 'Frank', the output should be '867-5309'. This requires splitting the input string into a dictionary where 'Frank' is a key, and '867-5309' is the associated value.

User Snakehiss
by
3.0k points