+ 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')
17 Respuestas
+ 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]
+ 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')
+ 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)
+ 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
+ 3
Contacts[0] will return a tuple i.e. ('James',42). So you should use Contact [0][1] to return the value of tuple.
+ 2
Thanks Python learner, the coversion worked
+ 1
Still doesn't work, look at Amanda, instead if Amanda is 63, it outputs Amanda is and then Amanda. 63
+ 1
And it wont let me concatenate strings with tuples
+ 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])
+ 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
+ 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?
+ 1
Do I just create dictionary with tuple values?
+ 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.
+ 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')
+ 1
Tried it for Amanda, still cant get it right, I understand what you mean, just cant figure out syntax for it
+ 1
But I used contact, instead of result
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')