mardi 24 février 2015

Reversing a sting codeacademy exercise (python)

I need to reverse a string without using the reverse function or [::-1].


My code so far is:



def reverse(text):
if len(text) == 1:
return text
else:
x= []
y= ""
for i in xrange(len(text)-1, -1, -1):
x[len(text) - i] = text[i]
for i in xrange(0,len(x)):
y[i] = x[i]
return y


The error I receive is "list assignment index out of range", although I am unsure why I am getting this. Thanks for any help.


Aucun commentaire:

Enregistrer un commentaire