How to use local variable outside loop

I have used the same code as the video but I am getting the following error. FYI, i have defined face_section in a ‘for loop’ and am trying to access it just after the end of the loop. As per my understanding, I should be able to do this is python.

Traceback (most recent call last):

File “face data collect.py”, line 47, in

cv2.imshow('face section', face_section)

NameError: name ‘face_section’ is not defined

Hey @mohitb, yes you will be able to use local variable outside its scope in python, but just make sure that line containing face_section = … , must run before cv2.imshow(‘face section’, face_section) in all conditions. So one easy hack you can use is, use face_section = np.zeros(100,100,3) initially before the for loop.

Hope this resolved your doubt. :blush:

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.