7/15 What am I doing wrong?


This is what I have for code so far:

def reverse(text):
text = str(text)
count = int(len(text)-1)
while count >= 0:
return text[count]
count = count - 1

When I print "count" then it'll give me the number that I want to index. However, when I attempt to index "text" at that point it fails. Does anyone know why this isn't working?


You must keep in mind that in python strings are immutable, so trying to do that will not work.

Python does have some built in string methods that will do that work for you though.

a = 'string'
a.replace('s', 'w')





Also once you use return in a function if it reaches it will terminate the function.


I misread again, let me clarifiy. If you want to reverse a sequence you have to handle it in a way that you can iterate through the data and return it in a revere order.

Use enumerate for an easy example.



def reverse(sequence):
    new = []
    for number, item in enumerate(sequence):
        new.append(sequence[((-1) * (number + 1))])
    return ''.join(new)

Try This:
def reverse(text):
    x = []
    for i in text:
    y = []
    while p!=len(x):
    return ''.join(y)