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). That list is followed by a name, and your program should output the phone number associated with that name. Ex: If the input is: Joe 123-5432 Linda 983-4123 Frank 867-5309 Frank the output is:

Respuesta :

Answer:

  1. contact_input = input("Enter contact list: ")
  2. contact_list = contact_input.split(" ")
  3. target = contact_list[len(contact_list) - 1]
  4. target_index = contact_list.index(target)
  5. print(contact_list[target_index + 1])

Explanation:

The solution code is written in Python 3.

Firstly, use input function to get the word pairs of contacts (Line 1).

Next, use split method to break the contact_input string into a list of individual name and contact number using single space " " as separator (Line 2)

Get the index of the target contact (Line 3 - 4). The desired target contact number can be found at target_index + 1 (Line 5).