Help with code please, it wont work | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

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[0]) if contact == 'Amy': print('Amy is' + contacts[1]) elif contact == 'John': print('John is' + contacts[2]) elif contact == 'Amanda': print('Amanda is') #print(contacts[3]) elif contact == 'Bob': print('Bob is' + contacts[4]) else: print('Not found')

31st Jul 2021, 8:39 PM
Michael Dhlamini
Michael Dhlamini - avatar
17 Answers
+ 7
contacts[0] will return the first tuple and to get the 2nd value from that tuple , you need to index it again , i.e. contacts[0][1]
31st Jul 2021, 8:48 PM
Abhay
Abhay - avatar
+ 5
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')
1st Aug 2021, 4:15 AM
Python Learner
Python Learner - avatar
+ 4
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)
31st Jul 2021, 11:25 PM
blackfish
blackfish - avatar
+ 3
Try this: https://code.sololearn.com/c7Lc7l0zeCGc/?ref=app I did the stuff that Abhay said above, added a capitalize function, because why not
1st Aug 2021, 1:34 AM
PresidentOfYes12
PresidentOfYes12 - avatar
+ 3
Contacts[0] will return a tuple i.e. ('James',42). So you should use Contact [0][1] to return the value of tuple.
2nd Aug 2021, 7:07 PM
Ankit Singh
Ankit Singh - avatar
+ 2
Thanks Python learner, the coversion worked
4th Aug 2021, 9:31 PM
Michael Dhlamini
Michael Dhlamini - avatar
+ 1
Still doesn't work, look at Amanda, instead if Amanda is 63, it outputs Amanda is and then Amanda. 63
31st Jul 2021, 8:53 PM
Michael Dhlamini
Michael Dhlamini - avatar
+ 1
And it wont let me concatenate strings with tuples
31st Jul 2021, 8:57 PM
Michael Dhlamini
Michael Dhlamini - avatar
+ 1
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[4]) or str(contacts[4][1])
31st Jul 2021, 9:03 PM
Abhay
Abhay - avatar
+ 1
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
31st Jul 2021, 9:22 PM
Michael Dhlamini
Michael Dhlamini - avatar
+ 1
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?
31st Jul 2021, 11:28 PM
Michael Dhlamini
Michael Dhlamini - avatar
+ 1
Do I just create dictionary with tuple values?
31st Jul 2021, 11:28 PM
Michael Dhlamini
Michael Dhlamini - avatar
+ 1
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.
31st Jul 2021, 11:44 PM
blackfish
blackfish - avatar
+ 1
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[0])) if contact == 'Amy': print('Amy is' + str(contacts[1])) if contact == 'John': print('John is' + str(contacts[2])) if contact == 'Amanda': print('Amanda is' + str(dict.get('Amanda')) if contact == 'Bob': print('Bob is' + contacts[4]) else: print('Not found')
1st Aug 2021, 12:06 AM
Michael Dhlamini
Michael Dhlamini - avatar
+ 1
Tried it for Amanda, still cant get it right, I understand what you mean, just cant figure out syntax for it
1st Aug 2021, 12:07 AM
Michael Dhlamini
Michael Dhlamini - avatar
+ 1
But I used contact, instead of result
4th Aug 2021, 9:34 PM
Michael Dhlamini
Michael Dhlamini - avatar
0
contacts = [ ('James', 42), ('Amy', 24), ('John', 31), ('Amanda', 63), ('Bob', 18) ] contact = input() for name, age in contacts: if contact == name: print(name +' is ' + str(age)) break else: print('Not found')
7th Nov 2022, 4:43 AM
callum matti
callum matti - avatar