Reverse


#1



I know how to reverse the string, my only issue is how do I make the "!" reverse as well?


https://www.codecademy.com/courses/python-intermediate-en-rCQKw/1/1?curriculum_id=4f89dab3d788890003000096#


Replace this line with your code. 

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


#2

def reverse(text):
    rev = ''
    count = len(text)
    while count > 0:
        rev += text[count - 1]
        count -= 1
    return rev
    print rev

This is how I did it, it is a bit neater.

Called a function reverse with argument text.
Set rev equal to an empty string.
Set the variable count equal to the length of the functions argument text.
Executed a while loop: while count is greater than 0
rev is equal to itself plus text count -1
count is equal to itself minus 1
return rev
print rev


#3

Thanks for the quick response!


#4

def reverse(text):
text='python’
if len(text)>1:
return reverse(text[1:])+text[0]

return text

Oops, try again. Your code looks a bit off–it threw a “maximum recursion depth exceeded” error. Check the Hint if you need help!


#5

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