vendredi 2 octobre 2020

How to use if statement is nested loop

In the following code with list T=[(0,3),(2,6),(1,9)], first iteration should be 3+6=9 and the second iteration should add output of previous iteration which is 9 to 9---> 9+9=18 and I want final output S=[3,9,18] but mine is S=[3,9,18,24,33]. It seems second loop is a extra loop but I wondering how to do it.

S=[]
S.append(T[0][1])            
b = T[0][1]
for i in range(len(T)-1):
      if T[i+1][0]-T[i][0]==1: 
       for i in range(1,len(T)):  
          b+=T[i][1]         
          S.append(b) 

Aucun commentaire:

Enregistrer un commentaire