我正在尝试计算“e”在一个单词中出现的次数。
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
它似乎工作正常,除非单词以“e”结尾。它会计算'e'两次。我不知道为什么。有帮助吗?
我知道我的代码可能很草率,我是初学者!我只是想弄清楚正在发生的事情背后的逻辑。
最佳答案
>>> word = 'eeeooooohoooooeee'
>>> word.count('e')
6
为什么不是这个?
关于python - 在python调试中计算单词中的字母,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4563552/