0% found this document useful (0 votes)
7 views

Computer Project

Uploaded by

mokogef899
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Computer Project

Uploaded by

mokogef899
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

SOURCE CODE

names = []

phone_numbers = []

num = 3

for i in range(num):

name = input("Name: ")

phone_number = input("Phone Number: ") # for convert to int =>


int(input("Phone Number: "))

names.append(name)

phone_numbers.append(phone_number)

print("\nName\t\t\tPhone Number\n")

for i in range(num):

print("{}\t\t\t{}".format(names[i], phone_numbers[i]))

search_term = input("\nEnter search term: ")

print("Search result:")

if search_term in names:

index = names.index(search_term)
SOURCE CODE
phone_number = phone_numbers[index]

print("Name: {}, Phone Number: {}".format(search_term,


phone_number))

else:

print("Name Not Found")

Variables

● names: An empty list to store the names entered by the user.


● phone_numbers: An empty list to store the corresponding phone numbers.
● num: The number of entries (name and phone number pairs) the program will accept.

Input Loop

The loop for i in range(num): runs num times (in this case, 3 times, since num is set
to 3). In each iteration of the loop:

1. The program prompts the user to enter a name using input("Name: ").
2. The program prompts the user to enter a phone number using input("Phone
Number: "). If you wanted to store the phone number as an integer, you could
convert the input using int(input("Phone Number: ")), but in this code, the
phone number is kept as a string.

These values are then appended to their respective lists:

● names. append(name) adds the entered name to the list.


● phone_numbers.append(phone_number) adds the corresponding phone
number to the phone_numbers list.

Displaying the Names and Phone Numbers


After the input loop, the program prints a formatted table with a header ("Name" and "Phone
Number"). The second loop for i in range(num): iterates through each index and
prints the name and phone number stored at that index.

Search Functionality

After displaying the list, the program asks the user to input a search_term using
input("\nEnter search term: ").

It then checks if the search_term exists in the names list:

● If the name is found (if search_term in names:), the program retrieves the
corresponding phone number using the index of the found name (index = names.
index(search_term)) and then prints the name and phone number.
SOURCE CODE
● If the name is not found, the program prints "Name Not Found."

Example Workflow
● The user is prompted to enter 3 names and phone numbers.
● The program displays the names and phone numbers entered in a table format.
● The user is then prompted to search for a name.
● The program displays the phone number associated with that name if found, or
"Name Not Found" if the name isn't in the list.

This code provides a basic structure for managing and searching a list of names and phone
numbers in Python.

4o

You might also like