+ 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) ]
44 odpowiedzi
+ 33
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.
+ 31
Everytime I think I know enough, no I don't
+ 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]}")
+ 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')
+ 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
+ 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.
+ 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")
+ 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]))
+ 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)
+ 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.
+ 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
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.
+ 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[0] == name:
age = contact[1]
print(f"{name} is {age}")
break
else:
print("Not found")
+ 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")
+ 1
contacts1 = dict(contacts)
a = input(" ")
if a in contacts1:
print(f"{a} is {contacts1[a]}")
else:
print("Not Found")
+ 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")
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))