why in this code the else output will print amd output is like that? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

why in this code the else output will print amd output is like that?

https://www.sololearn.com/compiler-playground/c82tB3jHW920 contacts = [ ('James', 42), ('Amy', 24), ('John', 31), ('Amanda', 63), ('Bob', 18) ] name =input() for k,v in contacts: if k==name: print(str(name)+" is "+str(v)) else: print("Not Found")

1st Feb 2023, 4:28 PM
Nariman Tajari
Nariman Tajari - avatar
14 Answers
+ 10
Nariman Tajari , > inside the loop in the if statement after printing, we need a *break* if the name is found. all the rest is ok. > the print() statement can be made a bit simpler: print(name, 'is', v)
1st Feb 2023, 4:50 PM
Lothar
Lothar - avatar
+ 8
Majdi Mbarek , your code needs to be corrected since the *else* statement has an indentation issue.
4th Feb 2023, 8:02 PM
Lothar
Lothar - avatar
+ 7
Nariman Tajari , your questiom seems to be incomplete. the post should include: > a clear task description with input / output sample > a description what exactly your issue is, (if possible including an error message)
1st Feb 2023, 4:45 PM
Lothar
Lothar - avatar
+ 7
Nariman Tajari , this what the python documentation says: Loop statements may have an else clause; it is executed when the loop terminates through exhaustion of the list (with for) or when the condition becomes false (with while), but not when the loop is terminated by a break statement. https://docs.python.org/3/tutorial/controlflow.html#break-and-continue-statements-and-else-clauses-on-loops then go to: 4.4. break and continue Statements, and else Clauses on Loops
1st Feb 2023, 5:51 PM
Lothar
Lothar - avatar
+ 5
Nariman Tajari Notice the indentation level places the else clause outside of the loop. That is what makes the else clause be tied to the for statement instead of the if statement.
1st Feb 2023, 5:36 PM
Brian
Brian - avatar
+ 4
Nariman Tajari this is known as a for/else statement. The else clause is tied to the for loop, not the if statement. For/else is useful when you expect to exit the loop with a break statement before it reaches the end of the loop. If the loop exits due to the break, then it skips past the else clause. Though if it does reach the end without breaking out early, then it executes the else clause. As Lothar pointed out, it is missing the break statement inside the if, so the loop reaches the end without breaking and it executes the else.
1st Feb 2023, 5:06 PM
Brian
Brian - avatar
+ 3
This will help you understand indentation better especially the first one from Hongfu
1st Feb 2023, 9:19 PM
Alex Wairegi
Alex Wairegi - avatar
+ 2
Brian answered your question too. "If the for loop reaches the end without breaking out early, then it executes the else clause."
1st Feb 2023, 5:31 PM
Ausgrindtube
Ausgrindtube - avatar
1st Feb 2023, 9:18 PM
Alex Wairegi
Alex Wairegi - avatar
+ 1
Lothar my question is as a routine behaviour of syntax when the if executee the else should not extlecute or even check but here the function acting strangely and check both of them
1st Feb 2023, 4:47 PM
Nariman Tajari
Nariman Tajari - avatar
+ 1
i appreciate for the response Lothar But i donno why the else part is being check here i know the rest
1st Feb 2023, 4:59 PM
Nariman Tajari
Nariman Tajari - avatar
+ 1
thanks Brian but what prints out after else i mean what thw interpreter send to else that cause to prints like that
1st Feb 2023, 5:11 PM
Nariman Tajari
Nariman Tajari - avatar
+ 1
you should use a (break) keyword after the (if) statement means if the condition is true print it and go out name =input() for k,v in contacts: if k==name: print(str(name)+" is "+str(v)) break else: print("Not Found")
3rd Feb 2023, 2:15 PM
Majdi Mbarek
Majdi Mbarek - avatar
+ 1
Lothar yes , i forget it , thank you
4th Feb 2023, 8:18 PM
Majdi Mbarek
Majdi Mbarek - avatar