7 Reverse


<Below this line, add a link to the EXACT exercise that you are stuck at.>
<In what way does your code behave incorrectly? Include ALL error messages.>
No error messages through the code, however on the codeacademy it says that the code is returning None, instead of “!nohtyp” However when I added my own version calling it, it does it for me. As you can see in the image below it works. What is wrong?


def reverse(text):
x = len(text)-1
new= “”
while x>=0:
new += text
x =x-1
print new


<do not remove the three backticks above><img src="//codecademy-discourse.s3.amazonaws.com/original/4X/c/5/e/c5e85b460af3367ef2e9ee6c2c260d865ee6710b.PNG" width="690" height="376">


The error message gives the expected solution… namely a return value that is not None.

    return new


Oh thank you. I was getting the same error on the next assignment, where it seemed to still be working as well. May I ask what the difference is between a return and a print?


We’re going to be discussiing functions, parameters and return values and their relative scopes.

In the above diagram there are four scopes, in all. The compute_bill() function exists in Global Scope. The red container is, Function Scope. The blue and green containers are each, Block Scope.

Notice where the return statement is? In function scope. A return statement in the other two would be also in that scope since they would have the same effect. Send a return value back to the calling scope, which in this instance is global.

Notice also that we sent groceries to the function as the parameter. This object exists in global scope and since it is a list, it is not copied so should not be mutated in the function (best practice). But we do iterate over it and peel off data which is accumulated in a local running total. total is in function scope. And, in this case, we are mutating the global object, stock which is accessible from inside the function.

The return statement passes this local value (not the variable total, though) back to the caller scope, where it is then printed.

The example shows that we could have printed before returning, or after with no apparent difference. The real difference though is that now we have the value in global scope.

your_bill = compute_bill(groceries)
print your_bill

Now we have it stored in this scope, as well, so may continue to use this value in further computation or tabulation. Making sense?


Thanks for taking the time to explain all this to us. You obviously put a lot of effort into that explanation, which is pretty sound of you. But I’m confused about the above line.

Why wouldn’t there be an (apparent) difference if we printed before returning.

And what exactly is the difference between those two ‘commands’. I mean to me printing seems to literally print to the display console, whereas return seems to close out a function (or something…)


Printing does not give anything back from the function. It is strictly output to the display. If the value is not returned, then the program has nothing to work with. By returning a value, the program does have something to continue on with.