Loop gives correct output if script run in steps but not when skript runs from scratch
Question: My script contains a while loop:

    
    
    import numpy as np
    
    
    ptf = 200 #profiltiefe
    dz = 5
    DsD0 = 0.02
    D0 = 0.16 #cm2/sec bei 20°C
    Ds= D0 * DsD0 
    eps= 0.3
    R= 8.314 
    Ptot=101300 
    Te = 20
    dt = 120 
    
    modellzeit = 86400*3 
    J=modellzeit/dt 
    PiA = 0.04 
    CA = PiA*1000/Ptot 
    
    respannual = 10 #t C ha-1 a-1
    
    respmol = respannual/12*10**6/10000/(365*24)
    respvol_SI = respmol * R * (Te+273)/(Ptot*3600) 
    respvol = respvol_SI * 100 
    
    
    I= ptf/dz 
    S = np.zeros(40)
    
    for i in range(40):
        if i <= 4:
            S[i] = respvol/(2*4*dz)
        if i > 4 and i <= 8:
            S[i] = respvol/(4*4*dz)
        if i > 8 and i <= 16:
            S[i] = respvol/(8*4*dz)
    
    
    Calt = np.repeat(CA,len(range(int(I+1))))
    Cakt = Calt.copy()
    
    res_out = range(1,int(J),1)
    
    Cresult = np.array(Cakt)
    faktor = dt*Ds/(dz*dz*eps) 
    
    timestep=0
    #%%
    while timestep <= J:
    
        timestep = timestep+1
    
        for ii in range(int(I)):
    
            if ii == 0:
                s1 = Calt[ii+1]
                s2 = -3 * Calt[ii]
                s3 = 2 * CA
    
            elif ii == int(I-1):
                s1 = 0
                s2 = -1 * Calt[ii]
                s3 = Calt[ii-1]
    
            else:
                s1 = Calt[ii+1]
                s2 = -2 * Calt[ii]
                s3 = Calt[ii-1]
    
    
            result = Calt[ii]+S[ii]*dt/eps+faktor*(s1+s2+s3)
            print(result)
            Cakt[ii] = result
    
        Cresult = np.vstack([Cresult,Cakt])    
    
        Calt = Cakt.copy()
    

What is intersting: If I run the complete script print(result) gives me
different (and incorrect) values. But if I add all my constants before and run
the loop part of the code (shown above) the loop performs well and delivers
the output I want.

Any idea why this might happen?

I am on Python 2.7.5 / Mac OS X 10.9.1/ Spyder 2.


Answer: You are using python 2.7.5, so division of integers gives integer results. I
suspect that is not what you want. For example, the term `respannual/12` will
be 0, so `respmol` is 0. Either change your constants to floating point values
(e.g. `respannual = 10.0`), or add `from __future__ import division` at the
top of your script.

