0

can someone help me with this for loops?

I’m confused with the second example. I came up with my mistake of forgetting to indent before print. In ex.1, I understand the logic. But in ex.2, how it end up being 1,3,6,10 ?? #ex.1 total2 = 0 for i in range(1,5): total2 += i print(total2) #ex.2 total2 = 0 for i in range(1,5): total2 += i print(total2)

26th Jan 2020, 7:35 AM
Rapeephong Ruenjitkit
Rapeephong Ruenjitkit - avatar
2 Answers
+ 4
total = 0 Inside for loop i=1 Now total = 0+1=1 Then i=2 Now total = 1+2=3 Then i=3 Now total = 3+3=6 Then i=4 Now total = 6+4=10 So 1 3 6 10 are printed because the print statement is a part of your for loop and executes every time i is in range of 5.
26th Jan 2020, 8:01 AM
Avinesh
Avinesh - avatar
+ 1
Greetings! your second instance of code throws an error. and how does it differ from the first one? for me, they are identical. what is the problem condition? write the text of the task
26th Jan 2020, 7:53 AM
Yaroslav Vernigora
Yaroslav Vernigora - avatar