I have tried to get the code but something seems wrong. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

I have tried to get the code but something seems wrong.

the task is to: The function(last2) will receive one parameter, a string (string). The function will count the number of times the last two characters from the string are found next to each other in the rest of the string. The occurrence at the end of the string doesn't count. this is the code I write def last2(string): total = 0 last = string[len(string)-1:] for i in range(len(string)-2): if string[i:i+1] == last: total += 1 return total But somehow it doesn't fulfill the task. is there anything missing or any corrections I should make. #it is a python problem

24th Nov 2016, 9:27 AM
__JAYMES
__JAYMES - avatar
2 Answers
0
the third line: change len(string)-1: to len(string)-2: as you require last two characters of the string the one with len(string)-2...second last...and len(string)-1...the last one.... the 5th line: it will be string[i:i+2]==last because string slicing do not include the last specified element...what you wrote will give you only s[i] and not s[i] and s[i+1]. I hope it will help.
29th Nov 2016, 5:16 PM
Varun Goyal
Varun Goyal - avatar
0
Thanks it helped
30th Nov 2016, 8:15 AM
__JAYMES
__JAYMES - avatar