Help with code please, it wont work
contacts = [ ('James', 42), ('Amy', 24), ('John', 31), ('Amanda', 63), ('Bob', 18) ] contact = input() if contact == 'James': print('James is' + contacts) if contact == 'Amy': print('Amy is' + contacts) elif contact == 'John': print('John is' + contacts) elif contact == 'Amanda': print('Amanda is') #print(contacts) elif contact == 'Bob': print('Bob is' + contacts) else: print('Not found')
7/31/2021 8:39:53 PMMichael Dhlamini
17 AnswersNew Answer
contacts will return the first tuple and to get the 2nd value from that tuple , you need to index it again , i.e. contacts
Convert this list of tuples to dictionary and use it as it should be use, e.g.: dic_contacts = dict((x,y) for x, y in contacts)
Try this: https://code.sololearn.com/c7Lc7l0zeCGc/?ref=app I did the stuff that Abhay said above, added a capitalize function, because why not
Hi Michael! You have many methods to achieve this practice. You can use if-else statements or for loop with dictionary so and so. You have to use dict() function to convert tuple to dictionary. Then you can declare its key as input variable. After that,use if-else to get the final output. contact = dict(contacts) key = input() if key in result: print(key, "is", contact[key]) else: print("Not found") If you want to complete this practice by using for loop, then here it is contact = input() for name, age in contacts: if contact == name: print(name +' is ' + str(age)) break else: print('Not found')
Contacts will return a tuple i.e. ('James',42). So you should use Contact  to return the value of tuple.
Thanks Python learner, the coversion worked
Still doesn't work, look at Amanda, instead if Amanda is 63, it outputs Amanda is and then Amanda. 63
And it wont let me concatenate strings with tuples
Yes you can't just concatenate any two different types of object in python . It expects tuple to be a string as well , if you want to concatenate tuple then try to convert it to string type by str(contacts) or str(contacts)
Im given a tuble list ib the form.of a dictionary, input is supposed to be the key and program must return value associated with key
The tuple is given in the question, I think Im supposed to use it like that but I think I agree with you, it seems impossible, how do I convert?
Do I just create dictionary with tuple values?
The last line in my previous answer converts the list of tuples to dictionary. Then using get() you don't have to use all these if-else statements, but just one print with user's input.
contacts = [ ('James', 42), ('Amy', 24), ('John', 31), ('Amanda', 63), ('Bob', 18) ] contact = input() dict = dict((y, x) for (x, y) in contacts) if contact == 'James': print('James is' + str(contacts)) if contact == 'Amy': print('Amy is' + str(contacts)) if contact == 'John': print('John is' + str(contacts)) if contact == 'Amanda': print('Amanda is' + str(dict.get('Amanda')) if contact == 'Bob': print('Bob is' + contacts) else: print('Not found')
Tried it for Amanda, still cant get it right, I understand what you mean, just cant figure out syntax for it
But I used contact, instead of result