Reverse


#1

i get the following error message and can't understand why:

Oops, try again. Your code looks a bit off--it threw a "string index out of range" error. Check the Hint if you need help!

am trying to loop from last character in text to the 1st and add them to variable s which is a string.

anyone?:slight_smile:

def reverse(text):
    s = ''
    for i in range(len(text),1,-1):
        s = s + text[i]
    return s


#2

The end of your applicable range is 0 so the bound would be -1.


#3

So, what should i change? don't quite follow that :slight_smile:


#4

The end parameter, -1.


#5

well..changed it to
for i in range(len(text),-1,-1):

and still get the same error :frowning:


#6

if i change to:

def reverse(text):
s = ''
x = len(text)
for i in range(0,x):
s = s + text[i]
return s

i get the original text string...when i change the range to:

range(x,0,-1)
or
range(x,-1,-1)

i get that error...


#7

solved it:

def reverse(text):
s = ''
x = len(text)
for i in range(x-1,-1,-1):
s = s + text[i]
return s


#9

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.