+ 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")
14 Réponses
+ 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)
+ 8
Majdi Mbarek ,
your code needs to be corrected since the *else* statement has an indentation issue.
+ 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)
+ 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
+ 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.
+ 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.
+ 3
This will help you understand indentation better especially the first one from Hongfu
+ 2
Brian answered your question too.
"If the for loop reaches the end without breaking out early, then it executes the else clause."
+ 2
see this code :
https://code.sololearn.com/cT5BRIbkia21/?ref=app
additional references.. hope it help to solve it...
https://www.sololearn.com/discuss/2983351/?ref=app
https://www.sololearn.com/discuss/2534829/?ref=app
+ 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
+ 1
i appreciate for the response Lothar But i donno why the else part is being check here i know the rest
+ 1
thanks Brian but what prints out after else i mean what thw interpreter send to else that cause to prints like that
+ 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")
+ 1
Lothar
yes , i forget it , thank you