algorithm – what´s the problem with this for-if loop?

i was doing this code and i got a problem, i don´t know why, but python 3 doesn´t recognize
“endif” or “endfor”. i want the program return me a vector e. nevermind about the variable i(im gonna use later) and i know i am not ussing a los of constants.
the program return me: name ‘endif’ is not defined and name ‘endfor’ is not defined

what can i do ?
thanks!

D=2
N=32
N_i=(4*(10**7)) 
f=np.zeros(N)
B=np.zeros((N,N))
B(:,:) = 0
B(1:N-2,1:N-2) = 0
z_k=5
C=np.zeros((N,N))
Z_c = 0.25 
dB  = 1   
s=2*D+1
seed = 12 
e=np.zeros(N_i+1)
len = e
g   = 1.
e_0 = (2.*D/s)*Z_c**2.
k1=1 
k2=N-2
j1=1 
j2=N-2

#BB = fltarr(N,N,1e5+1)



for i in range (1,1000):
e(i)=0;
for k in range(1,N-2):
    for j in range(1,N-2):
        Z_k = B(k,j) - (1/(2*D))*(B(k+1,j)+B(k-1,j)+B(k,j+1)+B(k,j-1));
        if abs(Z_k) > Z_c:
            C(k,j)   = C(k,j)   - (2*D/s)*Z_c*(Z_k/abs(Z_k));
            C(k-1,j) = C(k-1,j) + (1/s)  *Z_c*(Z_k/abs(Z_k));
            C(k+1,j) = C(k+1,j) + (1/s)  *Z_c*(Z_k/abs(Z_k));
            C(k,j-1) = C(k,j-1) + (1/s)  *Z_c*(Z_k/abs(Z_k));
            C(k,j+1) = C(k,j+1) + (1/s)  *Z_c*(Z_k/abs(Z_k));
            g         = (2*D/s)*(2*abs(Z_k)/Z_c - 1)*(Z_c**2);
            e(i)      = e(i)+g
        endif
    endfor
endfor