使用一个 for 循环来遍历单词中的每个字母,并检查相邻两个字母是否相同。如果找到连续相同的字母,函数将返回 true,否则返回 false。
示例代码如下:
def has_consecutive_letters(word):
for i in range(len(word) - 1):
if word[i] == word[i+1]:
return True
return False
测试代码:
print(has_consecutive_letters("banana")) # True
print(has_consecutive_letters("hello")) # True
print(has_consecutive_letters("world")) # False
print(has_consecutive_letters("python")) # False