- 1

Loops count

Why the code not work properly https://code.sololearn.com/cI8302cDDwuA/?ref=app

2nd Sep 2021, 3:11 AM
Adi Purnomo
Adi Purnomo - avatar
4 Réponses
+ 1
In the string "testing for loops", 't' occurs two times. According to your loop, it searches for 't' in the string and whenever it encounters a 't', it increases the value of variable "count". Now, with +=1, the value of "count" is 1+1 = 2. But, with +=9, the value of "count" is 9+9 = 18.
2nd Sep 2021, 3:19 AM
UD∆Y ∆.🇮🇳 [Inactive]
0
Because count starts at 0. There are 2 't's in your string. The for loop says: for each letter in the string, if the letter is 't', then take whatever count is and add 9 to it. 9 * 2 = 18
2nd Sep 2021, 3:16 AM
Slick
Slick - avatar
- 1
for x in str: if(x == 'o'): count += 1 so if I choose the word 'o', there's 3. so it's multiplied by 3 right? then the meaning of count += 1 I think only 1 is added
2nd Sep 2021, 3:21 AM
Adi Purnomo
Adi Purnomo - avatar
- 1
technically it adds or increments each iteration it finds a match. But yes
2nd Sep 2021, 4:03 AM
Slick
Slick - avatar