+ 1

How do count Affect the function. What is 0 and 1. Is this just INDEX or just counting number like WHOLE NUMBER.

#DIVISIBLE BY TEN def div_ten(num) : count= 0 for number in num: if (number% 10==0) : count+=1 return count print(div_ten([20, 25, 30, 35, 40])) OUTPUT is 3. My point: if count =0 then count+=1 should be equal to 1 only becAuse it meAns ---count+1= 0+1=1. Then how comes 3 in output. WhAt Am I missing. Kindly Check

29th Jan 2021, 10:48 AM
We Doru
We Doru - avatar
5 Antworten
+ 2
No, because the value of "count" from the start is 0. And from the start, the program still don't know how many numbers are there that is divisible by 10 before iteration. count = 0 **Iteration of 10** >> count += 1 count = 1 **Iteration of 30** >> count += 1 count = 2 **Iteration of 40** >> count += 1 count = 3 ___________ --> count = 3
29th Jan 2021, 10:58 AM
noteve
noteve - avatar
+ 2
This is counting the number of numbers in the list which is divisible by 10. This is called incremention. --> count += 1 is --> count = count + 1 So every iteration and if the condition is True the variable "count" is updated by adding 1 into it. 20, 30 and 40 are divisible by 10 therefore the condition if (number%10==0) will be True 3 times, and will increment the count 3 times by +1 as well. Therefore the final value of count will be 3.
29th Jan 2021, 10:52 AM
noteve
noteve - avatar
+ 1
Then there must be FOUR in OUTPUT because we hAve 3 elements divisible by 10. So, Counting must be 3+1= FOUR, which is updated vAlue. Is thAt how it mEAnt.
29th Jan 2021, 10:56 AM
We Doru
We Doru - avatar
+ 1
I guess you mean **iterAtion of 20** and not 10. Hope that's is correct.
29th Jan 2021, 11:02 AM
We Doru
We Doru - avatar
0
ThAnks much. Keep Up
29th Jan 2021, 11:09 AM
We Doru
We Doru - avatar