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


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))


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


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


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