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


#1

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 = []
         kata.append(text[n])
         print ', '.join(kata)
         
 print reverse("Python!")

#2

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


#3

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):        
        kata.append(text[n])
    print ''.join(kata)
    return kata
        
print reverse("Python!")

#4

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


#5

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.


#6

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


#7

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


#8

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.


#9
  • EDIT - -

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