My code prints the right word, but it says that it's wrong


#1



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


I don't understand why it gives this error message. When I call the function using "Python!" it even prints the correct text.



letterlist = []
def reverse(text):
    totalletters = 0
    reversedword = ""
    for letter in text:
        totalletters += 1
    for letter in text:
        letterlist.append(letter)
    while totalletters > 0:
        reversedword = reversedword + letterlist[totalletters - 1]
        totalletters -= 1
  
    print reversedword
    
reverse("Python!")


#2

Also, I have tried adding a "return reversedword" right by the "print reversedword", but it somehow returns a different value than it prints. I don't understand how that works.


#3

Your function shouldn't print anything, it should return the correct result
If you printed one thing and returned another, then something changed about the thing you printed/returned between those actions

Or perhaps you printed and returned the same value, but it wasn't the right value and the submission test complained about it - and then you compared what was printed while your code ran, to what the submission test says your function returned while it was testing your function. Why would those two things be the same? That wouldn't be the same function call. (Meaning, your observation that it printed something different from what was returned isn't correct, something else happened and you misinterpreted that)


#4

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