0
How can I make this code easier? GPT helps me with this code, but I don’t understand what means Found and function For In. Inoob
contacts = [ ('James', 42), ('Amy', 24), ('John', 31), ('Amanda', 63), ('Bob', 18) ] key = input() found = False for name, age in contacts: if name == key: print (name, "is", age) found = True break if not found: print ("Not Found")
10 ответов
+ 3
Without knowing the original task I would solve this with a dictionary, because this is much easier and shorter than using a for-loop.
My approach you can see in the attached file:
https://code.sololearn.com/c9weukqgI9MA/?ref=app
+ 7
What is the task description for this code you search for?
+ 5
a bit late, but for the sake of completeness i have picked thd task description:
the task is an exercise from `python developer`, `collection types`, `practice tuples`.
task description:
Tuples
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
If the contact is not found, the program should output "Not Found".
+ 4
jyonnyKumarHTMLCoditer ,
please do not spam in a post with a code that has no relation to the original topic.
+ 4
Fidu King ,
please do not spam here. your post has no relation to the topic, and the code seems to be copied from someone else
+ 3
The code has an array containing names and ages stored as tuples in the array, it takes a key from the user and declares a boolean called found to False and uses a for loop to check if the name entered by the user is in the array, if it is found it prints the name and corresponding age and found to True and breaks out of the loop. If at the end of the loop the key is not found then the program prints Not found
+ 1
Thank you, you helped me
0
Looks like a drop down list lookuo code … for selecting a name from a drop down (or other type of) list and then using the referenced data in something else .
0
Just a simple name / age lookup
0
No