Reverse


#1

I'm having trouble getting my code to run. I'm getting a 'string index out of range' error. Any ideas? I'm simply setting a variable equal to the length of the string and using that to store the letters counting back to the start of the string.

def reverse(text):
    length = len(text)
    rev = []
    while length > 0:
        char = text[length]
        rev.append(char)
        length -= 1
    result = "".join(rev)
    print result
    return result

#2

char = text[length]

The length variable holds the length of the text.
But index starts from 0 so there is no value for text[length]


#3

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