Can you tell me how to solve this. Project name: Contact Search | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Can you tell me how to solve this. Project name: Contact Search

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 Hint: If the contact is not found, the program should output "Not found". Try: 👇👇 contacts = [ ('James', 42), ('Amy', 24), ('John', 31), ('Amanda', 63), ('Bob', 18) ]

22nd Mar 2021, 4:23 AM
Navraj Singh
Navraj Singh - avatar
44 Answers
+ 32
Disclaimers: my english is not so good and im new in programing, so sorry for mistakes I tried in this way: contacts = [ ('James', 42), ('Amy', 24), ('John', 31), ('Amanda', 63), ('Bob', 18) ] name = input () for x in contacts: if name in x: print (str (x[0]) + " is " + str (x[1])) break if name not in x: print ("Not Found") in this code "x" means one tuple, for loop will be iterate until thats find name in x, if its find code will print output and break loop so it will ignore "Not Found" line, and if iteration don't find name in x this will excecute last line of code.
28th Mar 2021, 8:51 PM
Piotr
Piotr - avatar
+ 31
Everytime I think I know enough, no I don't
14th Apr 2021, 3:07 PM
Veena Tirmal
Veena Tirmal - avatar
+ 9
A much simpler version without all the iteration your welcome 😊 contacts = [ ('James', 42), ('Amy', 24), ('John', 31), ('Amanda', 63), ('Bob', 18) ] contacts1 = dict(contacts) a = input(" ") print(f"{a} is {contacts1[a]}")
8th Apr 2021, 5:16 PM
i Cased
i Cased - avatar
+ 7
#my first cod contacts = [ ('James', 42), ('Amy', 24), ('John', 31), ('Amanda', 63), ('Bob', 18) ] name=input() i=0 while i<=len(contacts): if i==len(contacts): print("Not found") break elif contacts[i][0]==name: print(contacts[i][0],'is',contacts[i][1]) break i+=1 #### #the code after I understood that I can use dict () function to convert lists to dictionary contacts = [ ('James', 42), ('Amy', 24), ('John', 31), ('Amanda', 63), ('Bob', 18) ] cont = dict(contacts) name = input() if name in cont: print(name,'is',cont[name]) else: print('Not found')
15th Apr 2021, 12:13 AM
Serghei Paladii
Serghei Paladii - avatar
+ 6
name= input("") if contacts[0][0] == name: age = contacts[0][1] print (name + " is "+ str(age))#0 elif contacts[1][0] == name: age = contacts[1][1] print(name + " is "+str(age))#1 elif contacts [2][0] == name: age = contacts [2][1] print(name +" is "+ str(age))#2 elif contacts [3][0] == name: age = contacts[3][1] print(name +" is "+str(age))#3 elif contacts [4][0] ==name: age = contacts[0][4] print(name +" is "+ str(age))#4 else: print("not found") Thank me later :) I dont know why i did it like this but it works and that's all that matters
16th Jul 2021, 8:59 PM
Edward Marais
Edward Marais - avatar
+ 4
Navraj Singh list of contacts has tuples and each tuple has 0th and 1th value. So using loop just check whether 0th value equal to input, if yes then print value otherwise print "Not found" In this case if you don't print "Not found" then it's also ok. You will get all test case passed.
22nd Mar 2021, 5:43 AM
A͢J
A͢J - avatar
+ 4
name = str(input()) contacts = [ ('James', 42), ('Amy', 24), ('John', 31), ('Amanda', 63), ('Bob', 18) ] x = dict(contacts) if name in x: print(name+ " is", x[name]) else: print("Not Found")
10th Aug 2022, 11:28 AM
Abdul Ghafar
Abdul Ghafar - avatar
+ 3
Use this code to get output from typing import List, Tuple contacts: List[Tuple[str, int]] = [ ('James', 42), ('Amy', 24), ('John', 31), ('Amanda', 63), ('Bob', 18) ] i=input() for item in contacts: if i in item: print (str(item[0])+' is '+str(item[1]))
26th Mar 2021, 9:24 AM
HARSH SHAH
HARSH SHAH - avatar
+ 3
My solution is: contacts = [ ('James', 42), ('Amy', 24), ('John', 31), ('Amanda', 63), ('Bob', 18) ] name = input () result = "" for i in contacts: if name in i: result = str(i[0]) + " is " + str(i[1]) break else: result = "Not Found" print(result)
29th Jun 2021, 8:57 AM
Saida Humbert
Saida Humbert  - avatar
+ 2
N = input() for X in contacts: if N in X: print (str(X[0])+ " is " +str(X[1])) break else: print ("Not Found") #the else statement must be in line with for statement and not if statement.
9th Dec 2023, 4:44 PM
Shubham Karjee
Shubham Karjee - avatar
+ 1
For help find comments in course 4.1 lesson second part about tuples. It helped me a lot. I used for contact in contacts loop. It can be done without dictionary.
26th Mar 2021, 9:58 AM
Luiza
Luiza - avatar
+ 1
contacts = [ ('James', 42), ('Amy', 24), ('John', 31), ('Amanda', 63), ('Bob', 18) ] string=input(" ") for x in contacts: if string in x: print(str(x[0]),"is",str(x[1])) break if string not in x: print("Not found") We can get input from the user, and using for loop we can iterate until we search for the input we given, and use break to come out of the loop and using indices print the founded string's value, if not found print not found.
15th Jul 2021, 3:16 PM
PREETHI P
+ 1
HARSH SHAH I did the way you said but my solution is over looping.....my results is writen on five different lines... How do i stop over looping...please
6th Dec 2021, 3:14 PM
VICTOR NSENGIYUMVA
+ 1
name = input() age = 0 for contact in contacts: if contact[0] == name: age = contact[1] print(f"{name} is {age}") break else: print("Not found")
2nd Aug 2022, 10:09 PM
Morteza Abdollahi
Morteza Abdollahi - avatar
+ 1
My solution: contacts = [ ('James', 42), ('Amy', 24), ('John', 31), ('Amanda', 63), ('Bob', 18) ] name = input () for x in contacts: if name in x: print (str (x[0]) + " is " + str (x[1])) break if name not in x: print ("Not Found")
10th Nov 2022, 8:15 AM
Pooja Patel
Pooja Patel - avatar
+ 1
contacts1 = dict(contacts) a = input(" ") if a in contacts1: print(f"{a} is {contacts1[a]}") else: print("Not Found")
31st Dec 2022, 8:57 AM
MOHAMED KAMAL ALI ABDALLA
MOHAMED KAMAL ALI ABDALLA - avatar
+ 1
My solution, i think the simpliest, also works on every test: contacts = [ ('James', 42), ('Amy', 24), ('John', 31), ('Amanda', 63), ('Bob', 18) ] contactsDictionary = dict(contacts) name = input() if name in contactsDictionary: print(f"{name} is {contactsDictionary[name]}") else: print("Not Found")
12th Mar 2023, 12:15 PM
Mateusz Kos
Mateusz Kos - avatar
0
Bro i can't able to understand what to do but there is a small written code in question.
22nd Mar 2021, 4:30 AM
Navraj Singh
Navraj Singh - avatar
0
Here is my attempt, i would appreciate any help user=input() contacts = [ ('James', 42), ('Amy', 24), ('John', 31), ('Amanda', 63), ('Bob', 18) ] print(contacts[user] + "is" + contacts[user],"not found")
24th Mar 2021, 3:37 PM
Simisola Osinowo
Simisola Osinowo - avatar
0
contacts = [ ('James', 42), ('Amy', 24), ('John', 31), ('Amanda', 63), ('Bob', 18) ] name=input() age=0 for i in range(len(contacts)): if(contacts[i][0]==name): age=contacts[i][1] break if age==0: a="Not found" else: a=age print("{0} is {1}".format(name,a))
1st Jul 2021, 2:27 AM
Pavithra
Pavithra - avatar