Reverse task


def reverse(x):
    outcome = ""
    if type(x) != str:
        x = str(x)
    for i in range(0, len(x)+1):
        outcome = outcome + x[len(x)-i]
    return outcome

I still count figure out where i'm getting it wrong. I need your help




you need to subtract one, why? len(x) will start counting at zero, where as string indexes are zero indexed based, so you will need to subtract one to compensate for this difference


Maybe this will help you:

def reverse(text):
length = len(text)
word = []
while (length !=0):
length = length-1
lst = ''.join(word)
return lst
a = reverse('python!')
print a


an example:

for i in range (0,5): #iterates over 0,1,2,3,4 -- but not 5!!

if i is 0 in your first iteration, then x[len(x)-i] is x[len(x)], which is out of range, since the indices begin with 0 and end with len(x)-1