Trying to write a program that will count the number of matching letter pairs adjacent to each other ("mississippi" contains 3) and displays this number.
Not sure what I'm doing wrong, but I have to use strings, while loop and variables as part of the code. It seems to work for the first line and prints 3, and then displays the IndexError: string index out of range for the second string example.
def count_pairs(s):
index = 0
pairs = 0
letter = 0
nextletter = 0
while index < len(s):
letter = s[index]
index = index + 1
nextletter = s[index]
if letter == nextletter:
pairs = pairs + 1
index = index + 1
else:
index = index + 1
return pairs
print(count_pairs("ddogccatppig"))
print(count_pairs("dogcatpig"))
print(count_pairs("xxyyzz"))
print(count_pairs("a"))
source
share