0

i=0 x=0 while i<4: x+=i i+=1 print(x)

Answer is 6...but I can't figure it out how it is 6... please can anyone explain

28th Aug 2021, 8:50 AM
Lokesh
2 odpowiedzi
+ 3
Lokesh loop 0. x+=0 -> x==0 i +=1 -> i ==1 loop 1. x+=1 -> x==1 i +=1 -> i ==2 loop 2. x+=2 -> x==3 i +=1 -> i ==3 loop 3. x+=3 -> x==6 i +=1 -> i ==4 loop 4. i not <4 loop breaks x = 6
28th Aug 2021, 9:00 AM
Rik Wittkopp
Rik Wittkopp - avatar
+ 1
You have 4 iterations. So you have [([0+0]+1)+2]+3. That's 6. I put every iteration in braces for better readability.
28th Aug 2021, 8:57 AM
Knäcke Brot
Knäcke Brot - avatar