Triple nested loop python (While loop insde of for loop inside of while loop)

Ulrich Eckhardt ulrich.eckhardt at dominolaser.com
Fri Mar 1 07:00:42 EST 2013


Am 01.03.2013 09:59, schrieb Isaac Won:
> try to make my triple nested loop working. My code would be:
> c = 4
[...]
> while c <24:
>          c = c + 1

This is bad style and you shouldn't do that in python. The question that 
comes up for me is whether something else is modifying "c" in that loop, 
but I think the answer is "no". For that reason, use Python's way:

   for c in range(5, 25):
       ...

That way it is also clear that the first value in the loop is 5, while 
the initial "c = 4" seems to suggest something different. Also, the last 
value is 24, not 23.



>          while d <335:
>                  d = d + 1
>                  y = fpsd[d]
>                  y1 = y1 + [y]
>                 m = np.mean(y1)
>          m1 = m1 + [m]

Apart from the wrong indention (don't mix tabs and spaces, see PEP 8!) 
and the that "d in range(336)" is better style, you don't start with an 
empty "y1", except on the first iteration of the outer loop.

I'm not really sure if that answers your problem. In any case, please 
drop everything not necessary to demostrate the problem before posting. 
This makes it easier to see what is going wrong both for you and others. 
Also make sure that others can actually run the code.


Greetings from Hamburg!

Uli






More information about the Python-list mailing list