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?

19th Jan 2021, 7:02 PM
CHANDAN ROY
CHANDAN ROY - avatar
3 odpowiedzi
19th Jan 2021, 7:07 PM
rodwynnejones
rodwynnejones - avatar
+ 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
19th Jan 2021, 7:09 PM
Rik Wittkopp
Rik Wittkopp - avatar
+ 2
Thanks rodwynnejones Rik Wittkopp I was unaware of this *else in for loop thing*. Got it now. Thanks a lot again!!
19th Jan 2021, 7:12 PM
CHANDAN ROY
CHANDAN ROY - avatar