How do not an error (else out of if) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How do not an error (else out of if)

contacts = [ ('James', 42), ('Amy', 24), ('John', 31), ('Amanda', 63), ('Bob', 18) ] nam=input() for x in contacts : if nam in x: print (nam+" is "+str(x[1])) break else: print ("Not Found")

10th Feb 2023, 11:12 PM
Solopy
3 Answers
+ 3
You're missing the print part if the condition is true
11th Feb 2023, 1:00 AM
Ausgrindtube
Ausgrindtube - avatar
+ 2
Unlike other languages, Python invented the use of `else` conditional for loops. Please refer the below docs link, the 4.4 section in particular - "4.4 break and continue Statements, and else Clauses on Loops" https://docs.python.org/3/tutorial/controlflow.html
11th Feb 2023, 4:35 AM
Ipang
+ 1
Thank you so much
11th Feb 2023, 8:24 PM
Solopy