+ 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
6 odpowiedzi
+ 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)
+ 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(""))
0
Thank you very much visph!!🙏🙏
0
Iterating with for loops is easier
for tup in contacts:
for i in tup:
if input in i:
#the rest of your code
- 2
Who can solve the Bmi calculator in python