Um ... that's odd code!
Let's start with the loop itself. Suppose you have three pages in a book you need to read and for some reason they have been numbered as Page 0, Page 1, and Page 2. The number of the page acts as an "index" into the book: you want "Page n" you open the book and count pages from 0 to n until you reach it.
So to read all the pages, you need to look at each
book[index]
where
index
is zero for the first page, one for the second, and two for the last one.
Your code doesn't do that:
while i < len(s)-1:
It starts with zero, that's fine - but the limit test is "less than the number of items minus one". Since there are three items, the
len
of the collection
s
will be three, so three minus one will be two, so the loop will never process the last item.
Change it to
while i < len(s):
Now it will look at values of i between 0 (the start) and 2 (the end) inclusive.
But that's not all!
Why are you incrementing
i
twice each time round the loop?
result += s[i]
i += 1
i += 1
That means that the first time round the loop,
i
is zero, the second time it is two, and there are no more values to look at after that!
Get rid of one increment, so it looks at each item in turn!
And do yourself a favour: go here:
pdb — The Python Debugger — Python 3.9.0 documentation[
^] and read up on the python debugger - it will let you look at exactly what your code is doing while it runs, and that can make your life a whole load easier with problems like this!