Hi I am getting confused of how python is able to access a variable declared-instantiated inside try block from out of it. How is this working at memory level?
Here is a block of code in which I am getting confused. I am on Python 3.8
def demo_method():
try:
num = float(input("Please enter a positive number (floating point) :"))
except ValueError:
num = float(input("Please enter a positive number (floating point) :"))
print("Value Entered ", num) ##Doubt: Why is this num tagged to the value fetched in either try block or except block
demo_method()
In the above code snippet, num is declared inside try(local to itself) and also except(local to itself). How can print statement which is outside of both try and except able to access this variable.
I am from a java background and I new to python. I would really appreciate an elaborate answer. Thank You