+ 1

Tuple problem in Python intermediate

The problem wants me to solve it as a tuple, but I had to rewrite the code as a dictionary to get the proper output. How would I solve this by leaving it as a tuple? https://code.sololearn.com/c7ZBmPst4pIC/?ref=app

6th Oct 2023, 11:44 PM
Billy
13 Réponses
+ 7
Rik Wittkopp , the output of the code you provided does not respect the task description (which was not given by the op). Billy , sure, you can modify the given input data. the task can be done without problems by using a dictionary. > but the exercise is meant to use it as it is, using tuples. contacts = [ ('James', 42), ('Amy', 24), ('John', 31), ('Amanda', 63), ('Bob', 18) ] inp = input() for contact in contacts: if inp in contact: print(f'{contact[0]} is {contact[1]}') break else: print('Not Found')
7th Oct 2023, 6:35 PM
Lothar
Lothar - avatar
+ 4
The challenge has a list containing a group of tuples. Tuples can be sliced similar to lists, you you can iterate through the list items, & slice your tuples to produce a result. Example contacts = [ ('James', 42), ('Amy', 24), ('John', 31), ('Amanda', 63), ('Bob', 18) ] for x in contacts: print(x[0],"is",x[1],"years old")
7th Oct 2023, 5:00 AM
Rik Wittkopp
Rik Wittkopp - avatar
+ 4
Billy Next time you want a dictionary from a tuple like contacts, then there is no need to rewrite it. You can just convert it this way: contacts = dict(contacts)
7th Oct 2023, 8:32 AM
Jan
Jan - avatar
+ 4
Quantum What a great trick, thanks for that piece of info 😁👍
7th Oct 2023, 10:22 AM
Rik Wittkopp
Rik Wittkopp - avatar
+ 4
this is the task description fof this exercise: 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".
7th Oct 2023, 6:31 PM
Lothar
Lothar - avatar
+ 2
Thanks but it looks like they also changed it to a dictionary. I thought maybe there was an answer where you leave the given code alone
7th Oct 2023, 1:55 AM
Billy
+ 2
Lothar Thank you! I didn't recognize the f'' , but I was able to solve with this: name = input() for x in contacts: if name in x: print(name, "is", x[1]) break else: print("Not Found") One last question, why doesn't this also work in line 3 of my above for-loop: if name == x:
9th Oct 2023, 10:12 PM
Billy
7th Oct 2023, 12:03 AM
**🇦🇪|🇦🇪**
**🇦🇪|🇦🇪** - avatar
+ 1
You can use a for loop to loop through each tuple and do your checking.
7th Oct 2023, 2:02 AM
Wong Hei Ming
Wong Hei Ming - avatar
+ 1
Rik Wittkopp Thanks for your comment, but I couldn't figure out a way to use your method where the list is not printed. dict() worked well
7th Oct 2023, 5:44 PM
Billy
+ 1
I cant find a way to do the exercise by leaving it as tuples. This is how I solved using the dict() function: contacts = [ ('James', 42), ('Amy', 24), ('John', 31), ('Amanda', 63), ('Bob', 18) ] contacts = dict(contacts) name = input() if name in contacts: print(name, "is", contacts[name]) else: print ("Not Found")
7th Oct 2023, 6:42 PM
Billy
+ 1
Billy, try the code below and see for yourself. name = input() for x in contacts: print(name) print(x) print(name == x)
10th Oct 2023, 2:34 AM
Wong Hei Ming
Wong Hei Ming - avatar
+ 1
Wong Hei Ming Ohhh I get what I was doing wrong. It needed to be if name == x[0]: Thanks for all the help!!
10th Oct 2023, 5:19 AM
Billy