0

Here I am again with new problem. I want to do some calculation on each field row after row. The for loop which I tried is not giving me what I expected

#sum each 5 fields over the raw:
    for step in range (6, N, 5):
        
        for i in range (step-5, step, 1):
            Total2 += float(fields2[i])
            # do something
        
                     
        print i, step, Total2

for the first 'step' Total2 gives me the right value but for the 2nd and 3rd 'step' it adds all values from the begining.
i step Total2
5 6 232.5
10 11 506.0 <--- all i= 1 - 10 but should be 6-10
15 16 744.0 <--- all i= 1 - 15 but should be 10-15

Can you please help me on how to fix this?

chebude

2
Contributors
2
Replies
3
Views
9 Years
Discussion Span
Last Post by chebude
0

You don't initialize Total2 to zero for each run through the outer loop

#sum each 5 fields over the raw:
    for step in range (6, N, 5):
        Total2=0     <--------------------------
        for i in range (step-5, step, 1):
            Total2 += float(fields2[i])
            # do something
                     
        print i, step, Total2
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.