7. reverse


#1
def reverse(text):
    reversedtext = ""
    index = len(text)
    while index > 0:
        reversedtext.append(text[index]) 
        index -= 1
    return reversedtext

I'm getting a strange error: "Oops, try again. Your code looks a bit off--it threw a "'str' object has no attribute 'append'" error. Check the Hint if you need help!"


#2

'str' object has no attribute 'append'

We cannot use .append() on a string object. Define it as a list, then return the string using .join()

return ''.join(reversedtext)

#3

I figured it out, this code works! I discovered I also forgot that indices in stings and lists start at 0 not 1 lol

def reverse(text):
    newtext = ""
    index = len(text) - 1
    
    while index > -1:
        newtext += text[index]
        index -= 1
    return newtext

#4

3 posts were split to a new topic: Throws a "string index out of range" error


#6