Reverse


#1



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

I wound up scrapping this version of the function in favor of one that uses a while loop, but I'd like to know why this version didn't work. It produced the following error:

Oops, try again. Does your reverse function take exactly one argument (a string)? Your code threw a "unsupported operand type(s) for -: 'int' and 'str'" error.


def reverse(text):
    result = ""
    for x in text:
        result = result + text[len(text)-x]
    return result


#2

x is a letter (type string) from your text string

len(text) is a number (type integer), so then looking at your error message:

which is telling us we can't subtract string (x) from integer (len(text))


#3

Oh, I was expecting x to be the index of the string.


#4

no, for in loop will give you items from the list, not indexes. If you want indexes, use range() in the for in loop


#5

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