python - Except Finally - Operator -
i have curious question regarding except block, finally:
during first block of code works supposed work, meanwhile in second minimaly different first 1 gives error.
first:
def askint(): while true: try: val = int(raw_input("please enter integer: ")) except: print "looks did not enter integer!" continue else: print 'yep thats integer!' break finally: print "finally, executed!" print val
second:
def abra(): while true: try: v = int(raw_input('geben sie bitte einen integer ein (1-9) : ')) except : print 'einen integer bitte (1-9)' continue if v not in range(1,10): print ' einen integer von 1-9' continue else: print 'gut gemacht' break finally: print "finally, executed!"
abra()
open solutions - thanks
i believe confusion you've got else
in first example.
in "try/except" block areas try
, except
, else
, finally
valid. can misleading new programmer, else
not same else if
statement.
you can read here: python try-else
in second example, if
statement out of place should either outside of entire try/except/else/finally
block, or indented 1 of sections correctly.
for specific example, you'll want similar this:
def abra(): while true: try: v = int(raw_input('geben sie bitte einen integer ein (1-9) : ')) if v not in range(1,10): print ' einen integer von 1-9' continue except : print 'einen integer bitte (1-9)' continue else: print 'gut gemacht' break finally: print "finally, executed!"
although, might suggest remove else
avoid confusion (but argument better discussed in link above):
def abra(): while true: try: v = int(raw_input('geben sie bitte einen integer ein (1-9) : ')) if v not in range(1,10): print ' einen integer von 1-9' continue print 'gut gemacht' break except : print 'einen integer bitte (1-9)' continue finally: print "finally, executed!"
Comments
Post a Comment