Scope of variables

I have a doubt in video 2 of Python 03 - Functions at 9:49
why does it not give error in case of enclosure when value of x was printed but gave error when it was updated though in both cases the variable x was not defined in local scope ??

Hey @stutijain578, when you use the assignment operator inside any function,… then that variable becomes local to that function only. It does not look for x outside that function. So in this case when you do x = x+1, x becomes local to the inner function and hence error comes up saying that local variable referenced before assignment. So to tell the inner function that x is defined in the outer function, you use the keyword nonlocal.

I hope you get this. You asked a very good question. If you still have even slightest of doubt, refer this link. Feel free to get back to me anytime in case of doubts.

Happy Learning :slight_smile: