0
otal= 0 for i in [1,2,3,4,5] : total=total+1 print(total)
It give output 5.is it counting total elements of list
3 ответов
+ 2
Yes, total = total + 1
0=0+1
2=1+1
3=2+1
4=3+1
5=4+1
if you want to sum the elements, try:
total=total+i
+ 1
Yes, since you increase the value of total by 1 and not by the value. To get the sum you'll need to sum up:
list = [ 1, 2, 3, 4, 5 ];
... loop ...
total += list[index]