#๐Ÿ”’ Result when using Return or Print

14 messages ยท Page 1 of 1 (latest)

mellow vigil
#

In Codecademy Functions Challenge there is a simple question that asks to print 3 multiples and return the last multiple. But when I do the return part on other functions with an equation, I get an answer. But not with this specific question.

def first_three_multiples(num):
print(num)
print(num * 2)
print(3 * num)
return print( num * 3 ) <<<----

^^^ This works.

def first_three_multiples(num):
print(num)
print(num * 2)
print(3 * num)
return num * 3 <<<-----

^^^ This doesn't work.
I get the 3 print statements but NOT the return.

On every other return statement I've done so far, I have always used an equation in the return line.
I've even done the equation on the return line in the next few questions on the challenge with no issues.
But there are no print statements either.

Can anyone explain to me why along the return line I get an answer for one but not the other?

deep lichenBOT
#

@mellow vigil

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

pearl thistle
#

what do you mean, you don't "get" the return?

#

what's not happening that you expect to happen?

orchid quartz
#

See:

#

!return

deep lichenBOT
#
Return statement

A value created inside a function can't be used outside of it unless you return it.

Consider the following function:

def square(n):
    return n * n

If we wanted to store 5 squared in a variable called x, we would do:
x = square(5). x would now equal 25.

Common Mistakes

>>> def square(n):
...     n * n  # calculates then throws away, returns None
...
>>> x = square(5)
>>> print(x)
None
>>> def square(n):
...     print(n * n)  # calculates and prints, then throws away and returns None
...
>>> x = square(5)
25
>>> print(x)
None

Things to note

  • print() and return do not accomplish the same thing. print() will show the value, and then it will be gone.
  • A function will return None if it ends without a return statement.
  • When you want to print a value from a function, it's best to return the value and print the function call instead, like print(square(5)).
deep lichenBOT
#
Print and return

Here's a handy animation demonstrating how print and return differ in behavior.

See also: /tag return

ruby furnace
#

Is this the task?

#

Your second code looks right then. Probably you just clicked the wrong button or something.

deep lichenBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.