+ 2

Please help me with this python code. Why isn't it working? I'm stuck, it's not giving me the output I need.

How do I access an item from a list of tuples? Someone help pleasehttps://code.sololearn.com/cDYJ6fOvLINz/?ref=app

8th Feb 2021, 10:30 PM
Yemisi
Yemisi - avatar
6 Respostas
+ 4
# simple fix: contacts = [ ('James', 42), ('Amy',24), ('John', 31), ('Amanda', 63), ('Bob', 18) ] def search(name): c = { n[0] : n[1] for n in contacts } if name in c: print(name,"is",c[name]) else: print("Not found") search(input("")) # anyway, you could do better (by just iterating the tuple list)
8th Feb 2021, 11:08 PM
visph
visph - avatar
+ 2
Abhay Thank you so much 🥺 I just started taking the intermediate python course and that's the challenge in the fourth module. I'll go back to understand the basics again. Thanks 🙏
8th Feb 2021, 11:20 PM
Yemisi
Yemisi - avatar
+ 1
Yemisi Akins Learn the basics of how to work with dictionary and lists in python ,only then move on to solve such problems :) You can make use of filter function to get a tuple from list which contains valid name. After you have that tuple , print the age from it. def search(name): b=list(filter(lambda x:x if name in x else 0,contacts)) if b: print(name + " is " + str(b[0][1])) else: print("Not found") search(input(""))
8th Feb 2021, 11:03 PM
Abhay
Abhay - avatar
0
Thank you very much visph!!🙏🙏
8th Feb 2021, 11:23 PM
Yemisi
Yemisi - avatar
0
Iterating with for loops is easier for tup in contacts: for i in tup: if input in i: #the rest of your code
9th Feb 2021, 1:42 PM
Ireneo language
- 2
Who can solve the Bmi calculator in python
12th Apr 2021, 4:20 PM
Tolu Buraimoh
Tolu Buraimoh - avatar