0

Does the SoloLearn Python compiler have problems?

Hello, In Python Intermediate course, Tuples, there is a code practice about contact searching. I wrote the code and worked properly in some external compilers, but Sololearn's compiler, throws an error message. Why? Here is my solution code: contacts = [ ('James', 42), ('Amy', 24), ('John', 31), ('Amanda', 63), ('Bob', 18) ] getListItem = int(input()) if getListItem < len(contacts): name = contacts[getListItem][0] age = contacts[getListItem][1] contact = name + " is " + str(age) # concatenation does not work for Integers; Wrapped in str() print(contact) else: print("Not found")

15th Mar 2023, 11:43 PM
Aram
Aram - avatar
3 ответов
+ 4
The input is string.but you convert it to integer. if i look on your code, you try to search the position of the contact. but you have to make the code to search the name in the contact list
16th Mar 2023, 12:07 AM
Bertram Rayhan
Bertram Rayhan - avatar
+ 1
Aram You're welcome!
17th Mar 2023, 12:42 AM
Bertram Rayhan
Bertram Rayhan - avatar
0
Bertram Rayhan Thank you for your reply. I just thought we should access the elements using the indexes. After your reply, I solved it by changing my code to this: contacts = [ ('James', 42), ('Amy', 24), ('John', 31), ('Amanda', 63), ('Bob', 18) ] name = input() for i in contacts: # for example, "i" now is "('Amy', 24)" if name == i[0]: # "Amy" is the first element or index 0; So, inputed Amy == Amy element age = i[1] # Sure, i[1] is the second element in "('Amy', 24)"; the "age" of Amy contact = name + " is " + str(age) # concatenation does not work for Integers; Wrapped in str() print(contact) # Print the contact as "Amy is 24" break # Terminate the condition if the name is in the list and Don't run the else part else: print("Not Found") # Otherwise, if the name is not in the contact list, print "Not found"
16th Mar 2023, 12:34 PM
Aram
Aram - avatar