0

Need a help in Contact Search problem

Here’s my code: contacts = [ (‘James, 42), (‘Amy’ ,24), (‘John’, 31), (‘Amanda’, 63), (‘Bob’, 18) ] name = input() name_list = list() for i in range(5): name_list += contacts[i][0] if name in name_list: for x in range(5): if name == contacts[x][0]: print(“{} is {}”.format(name, contacts[x][1])) else: print(“Not found”) /// Problem is, I always get “Not found” as an output whether the name is in the list or not. I know it’s a complicated code, but why doesn’t it work?

18th Oct 2021, 6:42 AM
Nordo Fin
Nordo Fin - avatar
3 RĂ©ponses
+ 1
contacts = [ ('James', 42), ('Amy', 24), ('John', 31), ('Amanda', 63), ('Bob', 18) ] text = input() for i in contacts: if text in i: print(str(i[0]) + " is " + str(i[1])) break if text not in i: print("Not found")
18th Oct 2021, 6:52 AM
Rusiru Rathmina
Rusiru Rathmina - avatar
+ 1
contacts = [ ('James', 42), ('Amy', 24), ('John', 31), ('Amanda', 63), ('Bob', 18) ] text=input() for i in contacts: if text in i: print(str(i[0]) + " is " + str(i[1])) break if text not in i: print("Not Found") /// I think I have solved ur problem. ...:)
12th Nov 2022, 6:32 PM
Arun Balaji
Arun Balaji - avatar
0
try " if name not in name_list: " instead of else :)
18th Oct 2021, 6:50 AM
Rusiru Rathmina
Rusiru Rathmina - avatar