7. reverse


#1



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

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


does this not right?


def reverse(text):
    text = []
    count = len(text)
    for things in text:
        print text[count-1]
        count -= 1
        if count <0:
            break


#2

you yourself can check it.just call the function with an argument.

Error:your function fails on reverse("Python!"). It returns "None" when it should return "!nohtyP"

simply your function is not working according to the instruction.your function doesn't return anything.

here

def reverse(text):
    text = []

what are you doing here?suppose we pass Python! as an argument then what does it happen?

The argument you pass will be add to text.so
text="Python!"
but you transfer it as an empty list.so
text=[]
so count =0 now

and the other part is here

print text[count-1]

just think if you print how results come in the console?what can you do instead of printing it?


#3

def reverse(text):
    text = ""
    count = len(text)
    for things in text:
        return text[count-1],
        count -= 1
        if count <0:
            break

i fixed something here as you said, but reverse("Python!") only show none in console. What's next? ^^


#4

def reverse(text):
    text = ""
   count = len(text)

you did the same.but this time it's empty string.we have to work with text.how can we work with text if we make it empty?

for things in text:
    return text[count-1],

you are looping through empty string.

Also remember return terminates a function.whenever a return occurs function ends at that moment.so return shouldn't be inside for loop.
and you dont need ,

why don't you create a new empty string or list ? then you can loop through text and add to that instead of returning or printing that you can store.then you can return the value


#5

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