syntax - Python for-else relationship -
this question has answer here:
- why python use 'else' after , while loops? 13 answers
i can run below python script without errors.
for n in range(3): print n else: print "done"
but puzzled else without matching if.
not make sense.
can 1 explain why works ?
the else
clause of for
, while
only executes if loop exits normally, i.e. break
never run.
for in range(20): print if == 3: break else: print 'haha!'
and else
clause of try
executes if no exception happened.
try: = 1 / 2 except zerodivisionerror: do_something() else: print '/golfclap'
Comments
Post a Comment