Can you tell me how to solve this. Project name: Contact Search | Sololearn: Learn to code for FREE!

+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) ]

3/22/2021 4:23:57 AM

Navraj Singh

18 Answers

New Answer

+7

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.

+5

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.

+4

Everytime I think I know enough, no I don't

+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]))

+2

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]}")

+2

#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')

+2

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

+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.

+1

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)

+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.

0

Bro i can't able to understand what to do but there is a small written code in question.

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")

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))

0

contacts = [ ('James', 42), ('Amy', 24), ('John', 31), ('Amanda', 63), ('Bob', 18) ] name = input() print(name+" is "+str(dict(contacts).get(name, "Not found"))) ex 1: Input>>> James Output>>> James is 42 ex 2: Input>>> Ikram Output>>> Ikram is Not found

0

Try this, works perfectly: contacts = [ ('James', 42), ('Amy', 24), ('John', 31), ('Amanda', 63), ('Bob', 18) ] name = input() contact_dic = dict(contacts) x = contact_dic.get(name, "Not found") if x == "Not found": print(x) else: print (name, 'is', str(x))

0

This is my solution (no else block needed): contacts = [ ('James', 42), ('Amy', 24), ('John', 31), ('Amanda', 63), ('Bob', 18) ] result = 'Not found' name = input() for contact in contacts: if contact[0] == name: result = contact[0] + ' is ' + str(contact[1]) print(result)

0

contacts = [ ('James', 42), ('Amy', 24), ('John', 31), ('Amanda', 63), ('Bob', 18) ] name = input() if dict(contacts).get(name): print(f'{name} is {dict(contacts)[name]}') else: print('Not found.')

-4

name = input() for x in contacts: if name in x: print(name + ' is '+ str(x[1])) else: print('Not found')