7/15 What am I doing wrong?


#1

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?


#2

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')

OUTPUT:

'wtring'

EDIT:

@netmaster82613

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

EDIT:

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.

@netmaster82613

EXAMPLE:

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

#3
Try This:
def reverse(text):
    x = []
    for i in text:
        x.append(i)
    p=0
    y = []
    while p!=len(x):
        y.append(x[len(x)-(p+1)])
        p+=1
    return ''.join(y)