- 2

Why the answer is 6? Python Beginners

27.1 exercise 4/5 I=0 X=0 While I < 4: X+= I I+=1 Print(x)

5th Jul 2021, 1:22 AM
Darlie Alexandra Antúnez
Darlie Alexandra Antúnez - avatar
7 Antworten
+ 7
You can check quiz comments to understand the quiz clearly. For example, I got this from the top comment. x =0 , i =0 while i<4 x+ = i , i+ = 1 1st loop x = 0 + 0 = 0 i = 0 + 1 = 1 2nd loop x = 0 + 1 = 1 i = 1 + 1 = 2 3rd loop x = 1 + 2 = 3 i = 2 + 1 = 3 4th loop x = 3 + 3 = 6 i = 3 + 1 = 4( i not less than 4 and program stop here ) So, the value of x is 6.
5th Jul 2021, 1:39 AM
Simba
Simba - avatar
+ 3
because in the loop x get sum of each values of l: 0 + 1 + 2 + 3 == 6
5th Jul 2021, 1:25 AM
visph
visph - avatar
+ 2
x += l is same as x = x + l
5th Jul 2021, 1:26 AM
visph
visph - avatar
0
Thanks visph!
5th Jul 2021, 1:26 AM
Darlie Alexandra Antúnez
Darlie Alexandra Antúnez - avatar
0
Ohhhhhhh
5th Jul 2021, 2:02 AM
Darlie Alexandra Antúnez
Darlie Alexandra Antúnez - avatar
0
Thanks Simba
5th Jul 2021, 2:02 AM
Darlie Alexandra Antúnez
Darlie Alexandra Antúnez - avatar
0
If you want some other value in the X variable you can replace the l with some other value
5th Jul 2021, 3:41 AM
Sumit Kumar
Sumit Kumar - avatar