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

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

Thanks a lot that did it!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.