'Write a program that first takes in word pairs that consist of a name and a phone number (both strings), separated by a comma
I have the following prompt:
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
my code:
pn = str(input()).split()
search = str(input())
i=0
for i in range(len(on)):
if pn[i] == (search):
print([i+1])
The input is getting split into a name and number. When the code goes to check if the names are the same, it always returns false. I've tried using the re.split() method, but it didn't work.
Solution 1:[1]
You should split twice and second split char should be a comma s.split(",")
s = "Joe,123-5432 Linda,983-4123 Frank,867-5309"
for i in s.split():
temp = i.split(",");
print("name :", temp[0] , " number is :" , temp[1] )
Output
name : Joe number is : 123-5432
name : Linda number is : 983-4123
name : Frank number is : 867-5309
Solution 2:[2]
Try splitting around the comma when searching for the name:
pn = input().split()
search = input()
for i in pn:
val = i.split(',')
if val[0] == search:
print(val[1])
Solution 3:[3]
You need to have the following things in your program to meet the basic requirements
- Infinite loop to enter contact info until user stop entering
- List or Dictionary to hold entered info and for searching
The code can be like following
contacts = {}
while True:
info = input('Enter Contact Info or Leave empty to Stop Entering: ').split(',')
if len(info) > 1:
contacts[info[0]] = info[1]
else:
break
name = input('Enter name to search: ')
print(contacts[name])
Output is following
Solution 4:[4]
It seems like you're trying to store the data as an input, ask the user for a query (name of a person), and then respond with that person's phone number.
# Get the data
inputs = input("Enter the input. >>> ").split(sep=" ")
# Split the data into lists
for pos in range(len(inputs)):
inputs[pos] = inputs[pos].split(sep=",")
# Ask for a search query
query = input("Enter a name. >>> ")
# Check for the name in the first element of each item
for item in inputs:
if item[0] == query:
print(f"{query}'s phone number is {item[1]}.")
break
A sample data input, as called in line 2:
Enter the input. >>> John,12313123 Bob,8712731823
As of the search query line of the code, your inputs variable looks something like: [['John', '12313123'], ['Bob', '8712731823']]. The program will iterate through the items of inputs, where each item is a list of two strings, and then check if the first item of this sub-list matches the inputted query.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Inputvector |
| Solution 2 | Stanley |
| Solution 3 | Zain Ul Abidin |
| Solution 4 | preritdas |

