[Tutor] Do loop in Python

stm atoc stm.at.oc at googlemail.com
Wed Nov 30 22:32:58 CET 2011


Yes. Actually, I have changed it to this kine od script:
# == model loop ==

#Optione1
if True:
 z=zeros( (numlayers,) )
 thickness= (thickness*1.0)
 for l in layers:
   z = arange ((-thickness - h * l),0,dz)
##    z= t -h * l
 nu = num+ (0.001*exp(-0.005*(z+200.))*dz)

#Option2
if False:
 thickness = range(-200 , 0, 10) # a list from -200 to 0 with step 10
(0, 10, 20, ..., 190, 200)
 layers = range(1,11) # a list from 1 to 10
 for t in thickness:
   for l in layers:
    z = arange(( t + h * l ), 0, dz )
#zvalues = arange(-200.,0,dz)
 nu = num+ (0.001*exp(-0.005*(z+200.)))

plot(nu,z)


Then it seems it works.

it should have a trend to reducing values...
- Show quoted text -

On Tue, Nov 29, 2011 at 2:00 PM, Steven D'Aprano <steve at pearwood.info> wrote:
> stm atoc wrote:
>>
>> Thank you so much for your reply. It was very helpful information and
>> I used it in order to improve the program....
>>
>> Here is the new version of the program:
>>
>> zvalues = [-200]  # starting value
>> hvalues = [10]  # starting value
>> increments = [1, 1, 1, 1, 1, 1, 1, 1]
>> for N in increments:
>>       h = hvalues[-1] - N
>>       hvalues.append(h)
>>       z = zvalues[-1] + h
>>       zvalues.append(z)
>>       height = arange((z)*dz,0,dz)
>>       for z,when in enumerate(height):
>>           nuh.append(0.001 * exp(-0.005*(z+200.0))*dz) #turbulence
>> diffusivity m**2/s
>>           nu.append(num + nuh[z])
>
>
>
> I'm afraid I still don't know what the arange function is. Is that a
> function you have written yourself? However, I can see that it doesn't
> actually get used!
>
> You create an arange object, and call it "height".
>
>    height = arange((z)*dz,0,dz)
>
> You should insert a print statement after this line to see what value height
> is given, and check that it is what you expect it to be.
>
> Presumably height is some sort of list or sequence of values, because you
> next use it in a for-loop:
>
>    for z,when in enumerate(height):
>        ...
>
> So now we know that z takes on the values 0, 1, 2, 3, ... and when takes on
> the values from height, whatever they are. But in the rest of your code, you
> don't use when at all:
>
>        nuh.append(0.001 * exp(-0.005*(z+200.0))*dz)
>
>        nu.append(num + nuh[z])
>
> No when, hence the values from height aren't actually used. Strange.
>
> Also, what are dz and num? You use them both, but I can't see where they are
> defined or what value they have. Likewise nuh and nu, although I can guess
> they are probably lists because you append to them.
>
> Because I don't know what values to use, and I don't know what arange is, I
> can't run your code to see what it does. So I'm reduced to guessing.
>
> If I take a wild stab in the dark that dz is a small number, say, 0.01, I
> can see what values nuh gets:
>
>
> py> from math import exp
> py> dz = 0.01
> py> nuh = []
> py> for z in range(10):
> ...     nuh.append(0.001 * exp(-0.005*(z+200.0))*dz)
> ...
> py> from pprint import pprint
> py> pprint(nuh)
> [3.6787944117144236e-06,
>  3.6604463480401533e-06,
>  3.6421897957152333e-06,
>  3.624024298324903e-06,
>  3.6059494017307832e-06,
>  3.587964654059516e-06,
>  3.5700696056914737e-06,
>  3.5522638092495153e-06,
>  3.5345468195878014e-06,
>  3.5169181937806692e-06]
>
> Is that the sort of behaviour you expect for nuh?
>
> Since the nuh values are changing, num+nuh[z] should also be changing, which
> implies nu should be changing.
>
> Unless num is so large that rounding error wipes out the nuh values.
>
>
>
>
> --
> Steven
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor


More information about the Tutor mailing list