Reverse Help and a few other questions


#1

<PLEASE USE THE FOLLOWING TEMPLATE TO HELP YOU CREATE A GREAT POST!>

<Below this line, add a link to the EXACT exercise that you are stuck at.>
https://www.codecademy.com/courses/learn-python/lessons/practice-makes-perfect/exercises/reverse?action=lesson_resume&link_content_target=interstitial_lesson

<In what way does your code behave incorrectly? Include ALL error messages.>
Your function fails on reverse(“Python!”). It returns “!noht” when it should return “!nohtyP”.

<What do you expect to happen instead?>

Why do special symbols fail on this loop and how do i rectify?
Also when I change range to range(0,len(text)-1) it does not detect the special symbol.
Also would it be detected if the symbol was in the middle of the string instead?




def reverse(text):
    output = ""
    for x in range(1,len(text)-1):
        output = output + str(text[len(text) - (x)])
    return output
 



#2

The range should equal string length since you are indexing from the right.

>>> def reverse(text):
    output = ""
    for x in range(1, len(text) + 1):
        output = output + text[-x]
    return output

>>> reverse("Python")
'nohtyP'
>>> 

#3

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