[Reverse] I have no idea why my code still error?!


Help me please...
I'm confused why this code still error? Instead the output correct but not inline :confused:

 def reverse(text):    
     for n in range(len(text)-1, -1, -1):
         kata = []
         print ', '.join(kata)
 print reverse("Python!")


you need inline, because you need to return the reversed string

You have nearly all the right code. Why did you place kata = [] inside the loop? This means, each iteration the list becomes empty again and won't persist

after the for loop, join the list into a string and return the reversed string


okayyy thanks for correction
but the last thing i really confused is, what variable that must return
i have try return kata but it's error.

this is my last code

kata = []
def reverse(text):    
    for n in range(len(text)-1, -1, -1):        
    print ''.join(kata)
    return kata
print reverse("Python!")


why place kata = [] outside the function?

you join the list into a string, either return this directly or store the string after joining and return the string


kata = [ ] should neither be outside the function nor inside the for loop. That is where your mistake is. Also, the function should return and not print.


its perfectly fine to also print inside the function? If in the end, then the right thing is returned as well


because it will fill the string later, so i put it outside function. If i put inside for loop always become empty again.

It makes me dizzy cause when i run it show correct output


if both nzioker and i give hints/ask question about something, please acknowledge it. You neither answer my question, or give an indication that you tried to understand nzioker solution

you print a reversed string, but you return a reversed list. You should return a reversed string.

  • EDIT - -

thanks for the help. I'm done so it must be return rather than print it ahaha