Here's a Python program that allows the user to input n names and phone numbers and stores them in a dictionary. The program then allows the user to search for a particular name and prints its corresponding phone number if it exists in the dictionary:
# Initialize an empty dictionary to store names and phone numbers
phonebook = {}
# Ask the user for the number of entries to input
n = int(input("Enter the number of entries: "))
# Loop through n entries to input names and phone numbers
for i in range(n):
name = input("Enter a name: ")
phone = input("Enter a phone number: ")
phonebook[name] = phone
# Ask the user for a name to search
search_name = input("Enter a name to search: ")
# Search for the name in the phonebook dictionary
if search_name in phonebook:
print(f"{search_name}'s phone number is {phonebook[search_name]}")
else:
print(f"{search_name} is not in the phonebook")
Example Output of the Program:
Enter the number of entries: 3
Enter a name: John
Enter a phone number: 555-1234
Enter a name: Sarah
Enter a phone number: 555-5678
Enter a name: Mike
Enter a phone number: 555-9101
Enter a name to search: Sarah
Sarah's phone number is 555-5678