+ 2

Please i need someone to help me explain this code from line 1 to the last line. I just came across it but i don't understand it

def pascals_triangle (number): for i in range (number+1): a= " " for j in range (number-i): a=a + " " for k in range (i): a=a + "3" Print (a) pascals_triangle (5) Results: 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3

5th Apr 2020, 10:32 AM
Okonkwo Emeka
2 Answers
+ 4
If number=5 then, First line print as below: i=0 a=" " at second for loop, j=0 then, a=" "+" " (because before a=" ") Now,a=" " (two spaces) j=1 then, a=" " (three spaces) j=2 then, a=" " (four spaces) j=3 then, a=" " (five spaces) j=4 then, a=" " (six spaces) At third for loop, Nothing do because i=0. First line has six spaces. i=1 a=" " at second for loop, j=0 then, a=" "+" " (because before a=" ") Now,a=" " (two spaces) j=1 then, a=" " (three spaces) j=2 then, a=" " (four spaces) j=3 then, a=" " (five spaces) At the third for loop K=0 then, a=" "+" 3"(space before3) Therefore a=" 3" Second line ptints ' 3' i=2 a=" " at second for loop, j=0 then, a=" "+" " (because before a=" ") Now,a=" " (two spaces) j=1 then, a=" " (three spaces) j=2 then, a=" " (four spaces) At the third for loop K=0 then, a=" "+" 3" K=1 then, a=" 3 3" Therefore a=" 3 3" Third line ptints ' 3 3' Like above, The output is, 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3
6th Apr 2020, 4:38 AM
Eshan Gayanga
Eshan Gayanga - avatar
+ 1
Wow.. Thank you very much. This is really helpful
10th Apr 2020, 12:18 AM
Okonkwo Emeka