Contact Search | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Contact Search

Hey, guys. Please help with the task. I do not understand at all how to extract a separate element in this task. I got something like this: contacts = [ ('James', 42), ('Amy', 24), ('John', 31), ('Amanda', 63), ('Bob', 18) ] name = input() if name == contacts[0] print("James is 42") But it does't work. I don't understand

3rd Jun 2021, 4:52 PM
Andrey Zinin
11 Answers
+ 5
contacts = [ ('James', 42), ('Amy', 24), ('John', 31), ('Amanda', 63), ('Bob', 18) ] x = input() for i in contacts: if x in i: print(f"{x} is {i[1]}") break else: print('Not found') ## else outside of the for loop, or it will put i "Not found"
18th Jul 2021, 4:39 PM
Clément Borie
Clément Borie - avatar
+ 2
contacts = [ ('James', 42), ('Amy', 24), ('John', 31), ('Amanda', 63), ('Bob', 18) ] name=input() count=1 for i in contacts: if i[0]==name : age= i[1] print(str(name) + " is " + str(age) ) break elif count>=len(contacts) : print("Not Found") count +=1
8th Oct 2022, 5:44 PM
Seyed Jalal Mazloomi
Seyed Jalal Mazloomi - avatar
0
This is task You are given a list of contacts, where each contact is represented by a tuple, with the name and age of the contact. Complete the program to get a string as input, search for the name in the list of contacts and output the age of the contact in the format presented below: Sample Input: John Sample Output: John is 31
3rd Jun 2021, 5:20 PM
Andrey Zinin
0
I mean that i can't understand how can I split the values in tuples
3rd Jun 2021, 5:22 PM
Andrey Zinin
0
Tuple can be accessed using the same method to access lists. Here is an example for accessing the values in the lists and tuples in this question: contacts[0] = ('James', 42) contacts[0][0] = James contacts[0][1] = 42 After you understand how to access the values, you can apply for loop to accomplish your task. Let me know if you have further questions.
9th Jul 2021, 9:12 AM
Xingguang Pan
0
Name=input () Contact_search:{ 'Umesh':21, 'Gokul':20, 'Lokesh':23, 'Charan':30, 'kalyan':40, 'Shanker':26, 'Dhoni':33 } print (Contact_search.get(Name,"Not found")
13th Jul 2021, 12:15 PM
Vadthyavath Kalyan
Vadthyavath Kalyan - avatar
0
On this same problem....i did as the person who asked the question ( at the top ) and my results are repeated on 5 lines ..eg. Amy is 24 Amy is 24 Amy is 24 Amy is 24 Amy is 24. What could be the problemClément Borie Jan Markus
6th Dec 2021, 2:59 PM
VICTOR NSENGIYUMVA
0
Can you help me on the question above please...Clément Borie
6th Dec 2021, 3:01 PM
VICTOR NSENGIYUMVA
0
contacts = [ ('James', 42), ('Amy', 24), ('John', 31), ('Amanda', 63), ('Bob', 18) ] name = input() if name in contacts[0]: x = contacts[0] y = x[0] b = x[-1] print(y + ' is' ,b) elif name in contacts[1]: x = contacts[1] y = x[0] b = x[-1] print(y + ' is' ,b) elif name in contacts[2]: x = contacts[2] y = x[0] b = x[-1] print(y + ' is' ,b) elif name in contacts[3]: x = contacts[3] y = x[0] b = x[-1] print(y + ' is' ,b) elif name in contacts[4]: x = contacts[4] y = x[0] b = x[-1] print(y + ' is' ,b) else: print('Not found')
5th Feb 2022, 6:33 PM
yura26
yura26 - avatar
0
The "solution" that Sololearn provides for this does not even solve all of the Test Cases. Seyed Jalal Mazloomi's solution in this thread provided solutions for all Test Cases.
18th Dec 2022, 10:41 PM
Alex
0
That is very true the solution by Sololearn is not working for me.
8th Oct 2023, 10:32 PM
Kenneth ongwela
Kenneth ongwela - avatar