8 Answers
New Answercontacts = [ ('James', 42), ('Amy', 24), ('John', 31), ('Amanda', 63), ('Bob', 18) ] name=input() for x in contacts : if name in x: print(name + " is " +str(x[1])) break else: print("Not found")
6/28/2022 4:24:55 PM
mahdi azizi8 Answers
New AnswerBecause x HAS to contain "name" before that block is even executed. It's in an if-statement, so it will only run once the tuple for that name has been found and the tuple only have 1 name each.
This is the pseudocode I came up with from looking at the code: //You have a list of tuples that contain names and ages //You take a name from the user //For each tuple (represented by x) in the contacts list, • if a the name the user input is present within the list: output the name in a format of "[name] is [age]" and then break the loop. • if the name is NOT present in the loop, you will output to the user "Not found"
It's okay, me too! That's why we are here to help! If it makes you feel better, I didn't understand what a function was and how it worked after I was using them for about 4 months. 😅
A block of code that can be used multiple times to produce a certain output. But I'm still a beginner so don't take my word for to much! 😂 I do have on my profile that I have no idea what I'm doing LOL.
Yes but How str(x[1]) will returns the age of corresponding name!!?? Why not of any other tuples?
Sololearn Inc.
535 Mission Street, Suite 1591Send us a message