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")
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
What is the task description for this code you search for?
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".
jyonnyKumarHTMLCoditer , please do not spam in a post with a code that has no relation to the original topic.
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
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
Thank you, you helped me
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 .
Just a simple name / age lookup