Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Count letters in a word in python debug

I am trying to count the number of times 'e' appears in a word.

def has_no_e(word):     #counts 'e's in a word
    letters = len(word)
    count = 0
    while letters >= 0:
        if word[letters-1] == 'e':
            count = count + 1
        letters = letters - 1
    print count

It seems to work fine except when the word ends with an 'e'. It will count that 'e' twice. I have no idea why. Any help?

I know my code may be sloppy, I'm a beginner! I'm just trying to figure out the logic behind what's happening.

like image 578
Johnny Avatar asked Nov 27 '22 06:11

Johnny


1 Answers

>>> word = 'eeeooooohoooooeee'
>>> word.count('e')
6

Why not this?

like image 160
user225312 Avatar answered Dec 30 '22 09:12

user225312