0

Can anyone plz explain me in step by step of how this code works because I am unable to understand it.

https://code.sololearn.com/c0I3drZkNiPX/?ref=app

17th Oct 2020, 1:08 PM
zeeshan
zeeshan - avatar
2 Réponses
+ 2
Suppose the input is 7 First outer loop creates a range of 2,3,4,5,6,7,8 And sets term to 0 everytime it iterates over those values, so term will always be set to 0 for the first 7 numbers ,so we will look at when a is 8 ,then you have inner loop for b in range(1,a) which creates range of 1,2,3,4,5,6,7 ,iterates over them and add them one by one to term and since it was the last iteration of outer loop ,loop terminates and we exit out of it with term holding a value of 28 ,
17th Oct 2020, 1:21 PM
Abhay
Abhay - avatar
0
for each a =2 to n+1 => fir each b= 1 to a-1 it finding sum of 1 to a-1. But in next outer iteration, you are resetting term=0, so finally only it remembering sum of 1 to n. Edit : check this sum = 0 n = int(input("How many terms: ")) for a in range(2,n+2): term = 0 for b in range(1,a): term += b print("term", a-1, ":", term) sum += term print("sum off",n,"term is", sum)
17th Oct 2020, 1:29 PM
Jayakrishna 🇮🇳