+ 1

Tuples

Where is the mistake in the code below? Tuples 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 If the contact is not found, the program should output "Not Found". 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 else: print("Not Found")

5th Jan 2024, 5:05 PM
Gaidar Uteshev
Gaidar Uteshev - avatar
5 Antworten
+ 5
Gaidar Uteshev , i would recommend a beginner friendly version that is using a helper variable *found* that can hold the status `True` or `False`. it is initialized with `False`. contacts = [('James', 42), ('Amy', 24), ('John', 31), ('Amanda', 63), ('Bob', 18) ] name = input() found = False # initialize the helper variable <<< for x in contacts: if name in x: #print(str(x[0]) + " is " + str(x[1])) # better use the next line, that is easier to read and understand than this line: <<< print(x[0], "is", x[1]) # <<< found = True # if name is found, we change the status to True break if found == False: # only when name is not found, the following print statement will be executed print("Not Found")
5th Jan 2024, 9:25 PM
Lothar
Lothar - avatar
+ 4
Suppose I enter "Amy" as name. Then your code will first check if "Amy" is in ("James", 42). As it is not, it will print that "Amy" is not found.
5th Jan 2024, 5:18 PM
Lisa
Lisa - avatar
+ 2
If you remove the indent from the else statement, it will become a for:else condition. In this situation, 'else' will fire if the for loop completes successfully. If the loop breaks (because your name is found) 'else' will not fire. Removing this indent allows your code to work. As a further challenge (not required) try to find a way to make it case insensitive.
5th Jan 2024, 6:26 PM
StuartH
StuartH - avatar
+ 1
Gaidar Uteshev , Just a tip for you to unlearn a bad habit taught by Sololearn, which is printing using str() and + and padding your strings with extra spaces. You rarely need to do any of that, because print does it automatically for you. I sent a suggestion, but they haven't fixed it, probably because it's found in many lessons. Instead of all this, print(str(x[0]) + " is " + str(x[1])) just do this. print(x[0], "is", x[1]) https://docs.python.org/3/library/functions.html#print [Edit:] I just realized Lothar already said the same thing inside a code comment. I'll leave my message in place though, to add weight to the suggestion and because I included the official docs where it explains what print does.
5th Jan 2024, 11:07 PM
Rain
Rain - avatar
+ 1
By this time you already know how to define a function. While Lothar's solution works, you can try a function approach as a challenge.
6th Jan 2024, 1:51 AM
Wong Hei Ming
Wong Hei Ming - avatar