sky_1 sky_1 - 1 month ago 22
Python Question

python keyword continue is shifting control to finally keyword

I am actually new to python. While learning it I came across this piece of code.

Python official document says when encounter with continue statement,
control will shift to the beginning of the loop, but in this case it is shifting to final statement and executing from there onward. Is this a bug in python or what? Can somebody please explain this to me? Thanks.

def askint():

while True:

try:
val =int(input("pleas enter an integer "))
except:
print ("it seems like you did'n enter an integer ")
continue
else:
print ("yep that's an integer thank you")
break

finally:
print ('control is now on finally me')
print ('i am also getting executed ')



askint()

Answer

The finally code is always executed in a try/except block.

The continue doesn't skip it (or it would be a bug in python).