Output Tuple | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Output Tuple

In the course "" this question ist asked: ################### 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 ################### As solution, this solution ist posted: ################### name = input() for x in contacts: if name in x: print(str(x[0]) + " is " + str(x[1])) break else: print("Not Found") ################### But this doesn't work, because in every case, the "Not Found" message is posted once. The question don't want to get a "Not Found", if a Name is found. How can I get the solution for it?

19th Aug 2023, 12:20 PM
Andreas Heil
Andreas Heil - avatar
2 Answers
+ 5
#Try this code for x in contacts: if name in x: print(x) break else: print("Not Found") #Note: The else block will NOT be executed if the loop is stopped by a break statement.
19th Aug 2023, 12:56 PM
SoloProg
SoloProg - avatar
+ 3
Hi Soloprog, thank you. That worked. 😀
19th Aug 2023, 1:09 PM
Andreas Heil
Andreas Heil - avatar