7/15 what is wrong?

> def reverse(text):
>     ls_text=[]
>     for index in range(0,len(text)-1):
>         ls_text.append(text[len(text)-1-index])
>     result=''.join(ls_text)
>     return result

> print reverse('abc')

the printed result was 'cb', not 'cba'.

So i changed
> range(0,len(text)-1)
> range(0,len(text))
then the code gave a desired result.
But i don't understand why the range should include len(text).
For example, in the case of inputting 'abc' to this function
> len(text)-1-index
would be -1 when the value of index go through 3.


Well, you need len(text) in you for loop if you don't know how long the string is going to be. The exercises runs multiply strings of a variety of lengths to see your code reverses the text correctly, so you you need to loop over the text no matter how long the string is


Hi @yunrori94 ,

If you are using indexes to iterate through a list, you need to use the len of the list to control the either the start or the end of the loop. When using the range function, remember that the end value of the range is the first value that is not included within the range, subject to the direction in which the iteration is proceeding.

If you are starting at index 0 and ascending through the indexes, the len of the list will be 1 higher than the last index, therefore it is an appropriate end value for the range that controls the loop.

If, instead, you are starting at the highest index in the list and descending down to 0, then subtract 1 from the len of the list to determine at which index to start the range. The len of the list is 1 higher than the highest index. You will be working down to index 0, so you would need to specify -1 as the ending value of the range, so that it stops at 0.


oh....i thought the end value of range function is also included within the range.
Thanks a lot ^^//