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

+11

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) + " is " + str (x)) 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.

+11

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

+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]==name: print(contacts[i],'is',contacts[i]) 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')

+6

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

+5

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.

+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)+' is '+str(item))

+3

name= input("") if contacts == name: age = contacts print (name + " is "+ str(age))#0 elif contacts == name: age = contacts print(name + " is "+str(age))#1 elif contacts  == name: age = contacts  print(name +" is "+ str(age))#2 elif contacts  == name: age = contacts print(name +" is "+str(age))#3 elif contacts  ==name: age = contacts 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

+2

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

+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) + " is " + str(i) 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),"is",str(x)) 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.

+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

+1

name = input() age = 0 for contact in contacts: if contact == name: age = contact print(f"{name} is {age}") break else: 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]==name): age=contacts[i] 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 == name: result = contact + ' is ' + str(contact) 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.')