Reverse


#1



Oops, try again. Your function fails on reverse("Python!"). It returns "!" when it should return "!nohtyP".


Replace this line with your code. 
def reverse(text):
    x=[]
    for char in text:
        x.append(char)
    y=[]
    a=len(x)
    while a>0: 
        a=a-1
        y.append(x[a])        
    for i in y :
        return i
what's wrong with my code?


#2

for i in y :
        return i

Your function returns first letter, and then stops. You shouldn't return every i, but store it in a variable.


#3

@dalachowsky is right, you will need to create a string so you can loop over the list and concatenate to string, or use build in function like .join()


#4

how do i use the built in function .join??


#5

https://www.tutorialspoint.com/python/string_join.htm

also consider to simply use a loop to concatenate to an empty string


#6

I solved that, by just changing the loop like that, and returning the result

for i in y :
        result += i

#7

thank you very much dalachowsky!!


#8

thank you very much stetim94!!


#9

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