Also tried an enumerate and a list comprehension. But I'll just focus on one thing and comment out the other methods I tried. I think maybe I was supposed to do something involving dictionaries?? But I have no idea how to do that. It wouldn't even let me define an empty dictionary. So the strategy I decided on is trying to write a program where if the user chooses a name, and that name is found in the list, the item in the index one to the right (which should be the appropriate phone number) will be returned. My for loop for this is towards the bottom.
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:
867-5309
''' Type your code here. '''
userEntered = input()
selectedName = input()
makeDict = {}
listLength = len(userEntered.split())
#for key, value in enumerate(makeDict):
#key = userEntered.split()[0:listLength:2]
#value = userEntered.split()[1:listLength:2]
#findNumber = [str(x) for x in userEntered.split() if str(x) == selectedName]
#for index, value in enumerate(userEntered.split()):
#if selectedName == value:
#print(value[index 1])
i = 0
for x in userEntered.split():
while i <= listLength:
if x == selectedName:
answer = x[i 1]
print(answer)
break
i =1
Enter program input (optional)
Joe 123-5432 Linda 983-4123 Frank 867-5309
Linda
Program output displayed here
(Your program produced no output)
EDIT: Solution now working with:
i = 0
for x in userEntered.split():
if userEntered.split()[i] == selectedName:
answer = userEntered.split()[i 1]
print(answer)
break
i =1
CodePudding user response:
Let me explain you what is not working in your program.
One problem is you inner while
loop. You use i
there but you don't use i
in condition, so it will always stop at first iteration or at last one. Even if it would work, you don't reset your i
between loops, so it stays the same after while
, which probably is not what you ment. If you remove while
loop completely it should work!
CodePudding user response:
You can use str.split()
to split the users/numbers and then zip()
to create the dictionary. For example:
userEntered = input().split()
selectedName = input()
dct = dict(zip(userEntered[::2], userEntered[1::2]))
print(dct[selectedName])
Prints (for example):
Joe 123-5432 Linda 983-4123 Frank 867-5309
Linda
983-4123
CodePudding user response:
Another method would be using regex with groups to look for a name and number pattern, you may also create a dictionary from the found results. Let's say you get the input as you have mentioned before:
userEntered = input()
selectedName = input()
which userEntered is as below:
Joe 123-5432 Linda 983-4123 Frank 867-5309
then you may call findall from re as:
import re
contacts = dict(re.findall(r"([a-zA-Z] )\s(\d -\d )", userEntered))
contacts[selectedName]
which prints the contact for selectedName, so if the selectedName was "Linda", it prints:
983-4123