7.reverse


#1



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

Oops, try again. Does your reverse function take exactly one argument (a string)? Your code threw a "'str' object is not callable" error.


 
def reverse(text):
    sum=""
    for i in range(0,len(text)-1):
        sum=sum+text([len(text)-1-i])
    print sum


#2

text() is a function call, but text is not a function, remove the parentheses


#3

def reverse(text):
    sum=""
    for i in range(0,len(text) -1):
        sum=sum+text[len(text)-1-i]
    return sum

modifying and running code it still give error like
Oops, try again. Your function fails on reverse("Python!"). It returns "!nohty" when it should return "!nohtyP".
https://www.codecademy.com/en/courses/python-intermediate-en-rCQKw/1/1?curriculum_id=4f89dab3d788890003000096


#4

you shouldn't subtract one from length of text because range does not includes its stop value


#6

my code looked almost the same as the one from OP
it helped me with what I was missing

only thing i don't get is the -i at

what does it do that make the code right?


#7

range() will gives us a list with integers (which if we do it right, match our indexes), this makes we get from end to start at the string

why not add a function call and print statements to your code, so you can actually see what your code is doing?


#8

ok,
if i delete the -i I'll get "!!!!!!!" (from printing 'Python!')
i understand that len(text)-1 the last index from the text and i represents the elements in this list

wich is kind of hard for me to get is how subtracting i from that index i get the itens on the opposite order

sorry if it is a dumb question


#9

i will increase, so this will cause the loop to go from right to left over the string.


Reverse task
#10

oh now I get it!!
thank you so much


#11

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