[solved] reverse


#1

When I run my function, allowing Codecademy to evaluate, I get the following output:

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

Note the added 'cba' appended to the output. However, if I add a print line and include the same input;

print reverse("Python!")

I see the expected output: !nohtyP

Not sure where the added 'cba' is coming from the Codecademy tests the function. I thought it might be related to how I initialize rev_text, and from reading other posts saw that I might try rev_text = None, but that also returned an error indicating; TypeError: unsupported operand type(s) for +: 'NoneType' and 'str'

My Code:

text_in = []
def reverse(text):
    for c in text:
        text_in.append(c)
        
    index = (len(text_in) - 1)
    
    rev_text = ""
    while index >= 0:
        rev_text = rev_text + text_in[index]
        index -= 1
    
    return rev_text
    print rev_text
    
print reverse("Python!")

Reverse
#2

the exercise validates your function works correctly by calling the function with different strings to validate all strings are reversed correctly

There is the catch: only the function is called, not the whole script.

Because you declare text_in outside the function, multiply function calls will cause text_in to keep strings from earlier function calls. Place text_in inside the function


#3

Not something I considered, and it was driving me crazy. Made the adjustment and it works as expected. Thank you for the input.


#4