Reverse


#1


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

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

What went wrong with my codes? Plz help!


def reverse(text):
    index = -1
    while index <= -len(text):
        print text[index],
        index -= 1
        
reverse('asdf')


#2

Your function needs to construct a new string that is the reverse of the input, then it should return it.


#3

A few things:
1) Your function is not returning anything (well, technically it is returning None).
2) On your print line, there is a comma at the end.
3) You are not concatenating all the letters in reverse order. You are just printing them one at a time.
4) For counting down on the index variable you need the less than equals to sign in your while statement reversed. -1 is greater than -7.


#4

Thank you! I got it revised and it worked!

def reverse(text):
    index = -1
    rev = ''
    while index >= -len(text):
        ind = text[index]
        rev += ind
        index -= 1
    return rev
print reverse(‘zxcvbnm')

#5

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