0
Contact Search
Hey, guys. Please help with the task. I do not understand at all how to extract a separate element in this task. I got something like this: contacts = [ ('James', 42), ('Amy', 24), ('John', 31), ('Amanda', 63), ('Bob', 18) ] name = input() if name == contacts[0] print("James is 42") But it does't work. I don't understand
11 Respostas
+ 5
contacts = [
('James', 42),
('Amy', 24),
('John', 31),
('Amanda', 63),
('Bob', 18)
]
x = input()
for i in contacts:
if x in i:
print(f"{x} is {i[1]}")
break
else:
print('Not found')
## else outside of the for loop, or it will put i "Not found"
+ 2
contacts = [
('James', 42),
('Amy', 24),
('John', 31),
('Amanda', 63),
('Bob', 18)
]
name=input()
count=1
for i in contacts:
if i[0]==name :
age= i[1]
print(str(name) + " is " + str(age) )
break
elif count>=len(contacts) :
print("Not Found")
count +=1
0
This is task
You are given a list of contacts, where each contact is represented by a tuple, with the name and age of the contact.
Complete the program to get a string as input, search for the name in the list of contacts and output the age of the contact in the format presented below:
Sample Input:
John
Sample Output:
John is 31
0
I mean that i can't understand how can I split the values in tuples
0
Tuple can be accessed using the same method to access lists.
Here is an example for accessing the values in the lists and tuples in this question:
contacts[0] = ('James', 42)
contacts[0][0] = James
contacts[0][1] = 42
After you understand how to access the values, you can apply for loop to accomplish your task. Let me know if you have further questions.
0
Name=input ()
Contact_search:{
'Umesh':21,
'Gokul':20,
'Lokesh':23,
'Charan':30,
'kalyan':40,
'Shanker':26,
'Dhoni':33
}
print (Contact_search.get(Name,"Not found")
0
On this same problem....i did as the person who asked the question ( at the top ) and my results are repeated on 5 lines ..eg. Amy is 24
Amy is 24
Amy is 24
Amy is 24
Amy is 24.
What could be the problemClément Borie Jan Markus
0
Can you help me on the question above please...Clément Borie
0
contacts = [
('James', 42),
('Amy', 24),
('John', 31),
('Amanda', 63),
('Bob', 18)
]
name = input()
if name in contacts[0]:
x = contacts[0]
y = x[0]
b = x[-1]
print(y + ' is' ,b)
elif name in contacts[1]:
x = contacts[1]
y = x[0]
b = x[-1]
print(y + ' is' ,b)
elif name in contacts[2]:
x = contacts[2]
y = x[0]
b = x[-1]
print(y + ' is' ,b)
elif name in contacts[3]:
x = contacts[3]
y = x[0]
b = x[-1]
print(y + ' is' ,b)
elif name in contacts[4]:
x = contacts[4]
y = x[0]
b = x[-1]
print(y + ' is' ,b)
else:
print('Not found')
0
The "solution" that Sololearn provides for this does not even solve all of the Test Cases. Seyed Jalal Mazloomi's solution in this thread provided solutions for all Test Cases.
0
That is very true the solution by Sololearn is not working for me.