写一个函数或计算双字母

Write a function or count double letters

我是编程新手,刚刚学习了字符串、切片和连接的基础知识。我的第一个任务是创建一个函数,该函数接受一组字符串和 returns 双字母的计数。我在 python 工作,一直在努力解决这个问题。

def twin(words):
    pairs = 0
    for i in range(len(words)-1):
        if words[i] == words[i+1]:
            pairs = pairs +1
        return words

这是我目前的想法

如果我们只计算背靠背的双字母,那么:

def twin(sentence):
    counter = 0
    for i in range(len(sentence) - 1):
        if sentence[i] == sentence[i+1]:
            counter += 1
    return counter


print(twin('Look book better'))