0

Can someone explain this for me? Why the answer will be 10?

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

3rd Jun 2017, 4:32 PM
黄美君
黄美君 - avatar
3 Respostas
+ 3
1+(1+1)+(1+1+1)+(1+1+1+1)
3rd Jun 2017, 4:45 PM
Calviղ
Calviղ - avatar
+ 2
@Calvin @Justin Hill Thanks alot!
4th Jun 2017, 5:48 AM
黄美君
黄美君 - avatar
+ 1
count = 0 for i in range (5): for j in range (i): count +=1 Everytime Your loop runs, it runs the second loop however many times the first loop is currently on. so 1st run of i runs j 0 times. count=0 2nd run of i runs j 1 time. count=1 3rd run of i runs j 2 times. count=2 count=3 4th run of i runs j 3 times. count=4 count=5 count=6 and your final run of i runs j 4 times. count=7 count=8 count=9 count=10 Run this code. I often use print functions to see what is happening. Maybe it will help clear it up count = 0 for i in range (5): print("i = {}".format(i)) for j in range (i): count +=1 print("count = {}".format(count))
3rd Jun 2017, 4:55 PM
LordHill
LordHill - avatar