Reverse - wondering why this approach doesn't work


Hey there! This is a pretty basic question, i'm definitely a beginner in Python, but I'm wondering what exactly is it about this solution won't work (below) because the logic seems reasonable to me.

Basically in order to get a function that can reverse the a string ("hello" becomes "olleh") I wanted to break up the string, then put it into a loop that iterates through every letter and inserts it into an empty list. So as it loops through, it inserts 'h' into the first spot in the list, but then 'e' gets inserted and so on until 'o' is actually in the first spot and everything is effectively reversed.

my code wont loop through every letter though, it stops at 'h'. Is my code off or does the idea just not work?

Please help! Thank you so much!

My code:
def reverse(text):
rev = [ ]
a = list(text)
print a
for l in a:
return rev



It looks like your program returns a list of strings ['o', 'l', 'l', 'e', 'h'] when the problem is asking for a single string 'olleh' .

Try using Python's built in function .join() on your list rev, to turn the list back into a string.

2 other points:
- print a is not needed for the program to work, you can leave this line out
- rev.insert(0,str(l)) can be changed to rev.insert(0,l)


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