Reverse


#1

def reverse2(text):
     x=len(text)-1
     f=""
     for i in text:
         f+=text[x]
         x-=1
     print f 
print reverse2("Why isn't this working?")

Why doesn't my above code work? I've run it and the results are the same as the following code, neonhight's code:

def reverse(text):
     rev_text = []
     final = ""
     for char in text:
         rev_text.append(char)
         dummy = list(rev_text)
     for letter in dummy:
         final += rev_text.pop()
     return final
print reverse("Why is this working?")

It's probably something simple I'm missing but I'd still like to know what I'm doing wrong.


Reverse
#2

the exercise wants you to return the result, but in reverse2 you only print, you do not use return


#3

Figured I'd share the method I used.. maybe it will help someone.

EDIT: Sorry about formatting

def reverse(text):
chars = []
chars2 = []
string = ""
counter = 0
for c in text:
chars.append(c)
for c in chars:
counter += 1
chars2.append(chars[len(chars)-counter])
string = "".join(chars2)
return string


#4

Whoops, my bad. You're right, thanks for pointing it out.


#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.