Someone please explain the output of the following code? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Someone please explain the output of the following code?

total = 0for i in 1, 3, 7: total = total + i print (total )

4th Sep 2016, 7:01 PM
brian
4 Answers
0
1,3,7 is a tuple. you could also write: for i in (1,3,7 ):
4th Sep 2016, 8:08 PM
Amarie
0
you start with total = 0 and i = 1. Then you defined the new value for total = 0 + 1 and print this value. After this you jump to the beginning of the loop. i = 3. The new value for total = 1+3 = 4 you print it and jump to the beginning. now is i = 7. The new value of total is total = 4 + 7=11. The loop is finished because in the tuple is no futher value. programm finished.
4th Sep 2016, 8:12 PM
Amarie
0
output will be 1 4 11
4th Sep 2016, 8:13 PM
Amarie
0
Thank you Amarie, could you kindly explain why variable total takes the value of the second total (1+3) for the next iteration?
6th Sep 2016, 7:55 AM
brian