+ 2

Python

count = 0 for i in range(20): for g in range(i): count += 1 print(count) can you guys please explain what the line " for g in range(i):" does in this coee because when i run the code without that line it gives "20" but if i run the code with the line it gives the answer "190"

3rd Mar 2018, 7:21 AM
Leonardo
Leonardo - avatar
2 Answers
+ 1
loop will run 20 times. and in that loop it will run a loop as many times as what the current loop cycle is. basically it goes like this:(using > to present loop change) 1 > 1+1 > 1+1+1 > 1+1+1+1 > 1+1+1+1+1 that is the sums of the first 5 loops of the 20
3rd Mar 2018, 12:07 PM
Markus Kaleton
Markus Kaleton - avatar
0
count = 0 for i in range(20): for g in range(i): count += 1 print(f" {i} {g} {count}") print(count) Veo que ya te lo explicaron muy bien. Para apreciarlo mejor. incluye la linea de impresion que yo añadi para veas como es el desarollo del codigo. Espero haber ayudado. SALUDOS.
15th Apr 2018, 2:57 PM
Luis Gonzalez
Luis Gonzalez - avatar