+ 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")
8 ответов
+ 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.
+ 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"
+ 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. 😅
+ 2
Thank you, sometimes I have difficulties in comprehending the most basic concepts
+ 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.
0
Yes but How str(x[1]) will returns the age of corresponding name!!?? Why not of any other tuples?
0
Now what exactly is a function😂
0
Well surely your doing better than me😅