Lesson 7. Reverse: Where is the mistake in this code?


#1




My code results in this error message: (" Oops, try again.
Your code looks a bit off--it threw a "list index out of range" error. Check the Hint if you need help! ")
Can you please explain my mistake or thinking error? Thank you very much!


I expect it to first add every letter of the text to the list and then overwrite each letter by the letter at the end of the text to create a reversed list. Finally, the code should convert the list back into a string by join function and return the converted string.


def reverse (text):
    lst= []
    for c in text:
        lst.append(c)
    n = 0
    while n<len(lst):
        lst[n] = lst[len(lst)]
        n+=1
    return "".lst.join


#2

I see your logic, how would your while statement exactly reverse the letters? You've got all the letters inside the lst list, and then you're overwriting the first elements in the list, and in some point you'll lose those first letters.

Try considering a cycle that starts bringing the elements from the end of the list of strings and stops with index zero. Remember the range() function?


#3

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