+ 2

If anyone explain this code step by step i will be very grateful

contacts = [ ('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")

28th Jun 2022, 4:24 PM
mahdi azizi
mahdi azizi - avatar
8 Answers
+ 3
Because 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.
28th Jun 2022, 4:45 PM
Justice
Justice - avatar
+ 3
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"
28th Jun 2022, 4:33 PM
Justice
Justice - avatar
+ 3
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. 😅
28th Jun 2022, 4:54 PM
Justice
Justice - avatar
+ 2
Thank you, sometimes I have difficulties in comprehending the most basic concepts
28th Jun 2022, 4:49 PM
mahdi azizi
mahdi azizi - avatar
+ 2
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.
28th Jun 2022, 4:58 PM
Justice
Justice - avatar
0
Yes but How str(x[1]) will returns the age of corresponding name!!?? Why not of any other tuples?
28th Jun 2022, 4:42 PM
mahdi azizi
mahdi azizi - avatar
0
Now what exactly is a function😂
28th Jun 2022, 4:56 PM
mahdi azizi
mahdi azizi - avatar
0
Well surely your doing better than me😅
28th Jun 2022, 7:17 PM
mahdi azizi
mahdi azizi - avatar