+ 3

How to access Tuples value

Im taking a Sololearn Practice, and Im stumped by this question: -------------------------------------------------- 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". ------------------------------------------------------------------ I tried it by doing the code below, but when I run it it always say "Not found:" contacts = [ ('James', 42), ('Amy', 24), ('John', 31), ('Amanda', 63), ('Bob', 18) ] cont = dict(contacts) name = input() if name in contacts: print (name + "is" + cont.get(name)) else: print ("Not Found") ----------------------------- I've seen others answer on this app but they look so complicated, and as a newbie I thought I could write it in a code I understand. However, Im stumped. Please tell me what Im doing wrong. And if there's other ways to make the code work without changing it too much.

6th Nov 2023, 3:44 AM
Jade Tito
Jade Tito - avatar
6 Réponses
+ 2
@Slick @Wong Hei Ming Thank you so. Turned out I cant concatenate string with ints. My bad. For anyone wondering, here's the working Code: contacts = [ ('James', 42), ('Amy', 24), ('John', 31), ('Amanda', 63), ('Bob', 18) ] cont = dict(contacts) name = input() if name in cont: print (name + "is" + str(cont.get(name))) else: print ("Not Found")
6th Nov 2023, 8:09 AM
Jade Tito
Jade Tito - avatar
+ 4
it's because of your if statement. You check the list of tuples for the name and not your created dictionary. try if name in cont: ... ...
6th Nov 2023, 3:53 AM
Slick
Slick - avatar
+ 3
The variable 'contacts' is a list of tuples. It is not very convenient for searching. Converting it to a dictionary, is helpful when there are pairs of keys and values, and all the keys are unique. It is a different data structure. Your 'cont' variable looks like this (compare with the tuple!): cont = { 'James': 42, 'Amy': 24, ... } So you can easily look up the value (age) that is associated to the key (age).
6th Nov 2023, 4:29 AM
Tibor Santa
Tibor Santa - avatar
+ 3
Jade Tito you can do the lookup without the need to convert to dict. contacts = [ ('James', 42), ('Amy', 24), ('John', 31), ('Amanda', 63), ('Bob', 18) ] name = input() for t in contacts: if name==t[0]: print (f"{t[0]} is {t[1]}") break else: print ("Not Found")
6th Nov 2023, 11:34 AM
Bob_Li
Bob_Li - avatar
+ 2
Accessing elements inside a tuple is the same as working with a list. Use the index.
6th Nov 2023, 4:28 AM
Wong Hei Ming
Wong Hei Ming - avatar
+ 2
Actually your code works with small modification. If you look at the error message, it says something like this: TypeError: can only concatenate str (not "int") to str It hints there is an integer in somewhere. Once you figure out where / what is it and convert it into a string... However this exercise is about tuple, I would not recommend changing the given code.
6th Nov 2023, 5:10 AM
Wong Hei Ming
Wong Hei Ming - avatar