Here is the code written by me to check if there are exactly 3 characters between the letters 'a' and 'b' in the string. I have tried the code with the following three strings:
- 'La Rob'
- 'lake boat'
- 'look after baby'
The code worked for the first 2 strings, output: True. However, the third one is saying: IndexError: string index out of range. Instead, the third string should cause two output: False. I know why is it saying that but can not fix it, what can be a solution here to fix it ?
def hi_coder(string):
for i in range(len(string)):
if string[i] == 'a' and string[i+4] == 'b':
return True
return False
return string
print(hi_coder(input()))