0
If/else confusion in python
result = int(input()) expressions = input().split(" ") for exp in expressions: if eval(exp) == result: result = expressions.index(exp) print ("index "+ str(result)) break else: print ("none") I was trying to solve a problem and I was stuck. Somehow I accidentally solved it by changing the indentation of else block. I didn't know that else block could be used without if block. Can somebody explain this to me?
3 odpowiedzi
+ 3
https://www.w3schools.com/JUMP_LINK__&&__python__&&__JUMP_LINK/python_for_loops.asp
scroll down to;- "Else in For Loop"
+ 3
you have a "for else" expression there.
The else is executed if you don't return a true result from the for expression
Example below:
https://code.sololearn.com/czWhMgIjlK4B/?ref=app
+ 2
Thanks rodwynnejones Rik Wittkopp I was unaware of this *else in for loop thing*.
Got it now. Thanks a lot again!!