0

I'm writing a program that have two nested for loops and and I wrote down my idea on a paper and drew a diagram for it and thought my logic was fine. But when I executed that I got something totally different that what I wanted. Then I created a small version of my program with only those two loops but the problem remains. so here is the smaller version I was testing

lopt = 0
dwl = 0.04
Dt = 0.01

for i in numpy.arange(0, 5):

    for t in numpy.arange(lopt, dwl, Dt):     #Inner loop (time)
        print 't = ', t, 'row = ', i


    lopt = t + 0.001

the output I got was

t =  0.0 row =  0
t =  0.01 row =  0
t =  0.02 row =  0
t =  0.03 row =  0
t =  0.031 row =  1
t =  0.032 row =  2
t =  0.033 row =  3
t =  0.034 row =  4

but what I want it to be is

t =  0.0 row =  0
t =  0.01 row =  0
t =  0.02 row =  0
t =  0.03 row =  0
t =  0.04 row =  0
t =  0.041 row =  1
t =  0.051 row =  1
t =  0.061 row =  1
t =  0.071 row =  1
t =  0.081 row =  1
t =  0.082 row =  2
t =  0.092 row =  2
t =  0.102 row =  2
t =  0.112 row =  2
t =  0.122 row =  2
t =  0.123 row =  3
t =  0.133 row =  3
t =  0.143 row =  3
t =  0.153 row =  3
t =  0.163 row =  3
t =  0.164 row =  4
t =  0.174 row =  4
t =  0.184 row =  4
t =  0.194 row =  4
t =  0.204 row =  4
t =  0.205 row =  5
t =  0.215 row =  5
t =  0.225 row =  5
t =  0.265 row =  5
t =  0.275 row =  5

my logic to it is: the outer loop starts with 0 then the inner loop goes from 0-0.04 then back to the final step of the outer loop by incrementing the variable lopt by 0.001. then back and execute one step of the outer loop again. now we get the inner loop one more time and start with time 0.041 to 0.081. this keep repeating until the outer loop is executed 6 times.

Another question I have is, at least in my current output the inner loop seems to be executed successfully in the first step, but it only go to 0.03. Since my range goes from 0 to 0.04 with increment of 0.01, shouldn't the loop runs for 0-0.01-0.02-0.03-0.04? also for the outer loop, shouldn't it run 0-1-2-3-4-5?

I'm sorry if i'm bugging you with this question but I honestly drew like a 8 diagrams for this problem and also did it with my hand and think it should run as intended. I feel really dumb because when I did by hand I just couldn't catch any flow in the logic

Mazin
  • 155
  • 1
  • 2
  • 9
  • You should also increment the upper limit of the range function as you progress. Try modifying `dwl` (as the loop continues) in your diagrams. – Rockybilly Feb 18 '18 at 00:39

1 Answers1

0

Firstly, the range returned by numpy.arange is not inclusive, so

for i in numpy.arange(0, 5):
    print i

will print 0 1 2 3 4 but not 5. In order to get it to print the 5, you want to increment it by the step, which is 1 in the case of the outer loop (by default) and 0.01 in the case of the inner one.

After the first execution of the inner loop, lopt is 0.03, which means that the arguments to the inner numpy.arange are (0.03, 0.04, 0.01) which results in only one loop. What you really want is

lopt = 0
dwl = 0.04
Dt = 0.01

for i in numpy.arange(0, 5):

    for t in numpy.arange(lopt, dwl + lopt + Dt, Dt):
        print 't = ', t, 'row = ', i

    lopt = t + 0.001

because that way the second evaluation of that inner numpy.arange will have arguments of (0.041, 0.09, 0.01), and the third will be (0.082, 0.131, 0.01) and so on.

Anonymous
  • 491
  • 2
  • 12