Missing print() in python -> basics-> functions -> return

how it is:

# Function definition on first line.
def add_two_numbers(number_one, number_two):
  result = number_one + number_two
  return result  # Returns the sum of the numbers.

>>> add_two_numbers(3, 4)
7

# This function will return None.
def add_two_numbers(number_one, number_two):
  result = number_one + number_two

>>> print(add_two_numbers(5, 7))
None

how it should be (in my opinion :grinning::

  • first command shoud have print() because the method returns a value and does not print it
# Function definition on first line.
def add_two_numbers(number_one, number_two):
  result = number_one + number_two
  return result  # Returns the sum of the numbers.

>>> print(add_two_numbers(3, 4))
7

# This function will return None.
def add_two_numbers(number_one, number_two):
  result = number_one + number_two

>>> print(add_two_numbers(5, 7))
None

The >>> implies this is running in the interactive Python environment/tool. The interactive Python ā€œshellā€ is a REPL environment ā€“ Read, Evaluate, Print, Loop. The P of REPL is print(). You donā€™t need an explicit print because the REPL explicitly does that for you.

I think this print should be removed: python/concepts/basics/introduction.md at 840f4012969ef4793c44ff7cb4b7d95e3d5ce041 Ā· exercism/python Ā· GitHub

1 Like

I can go ahead and do that, but removing print() means that you wonā€™t see the None being returned in the REPL. Calling the function without print will show the following:


>>> def add_two_numbers(number_one, number_two):
...   result = number_one + number_two
...
>>> add_two_numbers(3,4)
>>>

An alternative is to add more verbiage/example to show that the ā€œblankā€ is indeed None:

#Function definition on first line, explicit return used on final line.
def add_two_numbers(number_one, number_two):
  return number_one + number_two   

#Calling the function returns the sum of the numbers.
>>> add_two_numbers(3, 4)
7

#This function does not have an explicit return.
def add_two_numbers(number_one, number_two):
  result = number_one + number_two

#Calling the function appears to not return anything at all.
>>> add_two_numbers(5, 7)
>>>

#Using print() with the function call shows that 
# the function above is actually returning **None**.
>>> print(add_two_numbers(5, 7))
None

Thoughts?

See PR 3689, should you wish to finagle it. :slightly_smiling_face:

What happens if we add print to the first line? Thinking consistency is good if possible, but I get maybe itā€™s not easily achievable! :slight_smile:

I sorta feel like using print() a lot in examples encourages / implies that print() is the way functions are commonly called when thatā€™s not the case.

But I donā€™t feel strongly about it, really. Especially not in the first learning exercise.

The thing I do feel very strongly about is making sure students have a clear example of what happens when there isnā€™t a return in a function.

The errors associated with a function returning None in Python are super-common and really confusing for learners.

Variation one:

#Function definition on first line, 
# explicit return used on final line.
def add_two_numbers(number_one, number_two):
  return number_one + number_two   

#Using print() with the function call returns the sum of the numbers.
>>> print(add_two_numbers(3, 4))
7

#This function does not have an explicit return.
def add_two_numbers(number_one, number_two):
  result = number_one + number_two

#Calling the function directly appears to not return anything at all.
>>> add_two_numbers(5, 7)
>>>

#Using print() with the function call shows that 
# the function above is actually returning **None**.
>>> print(add_two_numbers(5, 7))
None

Variation Two:

#Function definition on first line, explicit return used on final line.
def add_two_numbers(number_one, number_two):
  return number_one + number_two   

#Using print() with the function call returns the sum of the numbers.
>>> print(add_two_numbers(3, 4))
7

#This function does not have an explicit return.
def add_two_numbers(number_one, number_two):
  result = number_one + number_two

#Using print() with the function call returns **None**.
>>> print(add_two_numbers(5, 7))
None

LMK which you prefer, or feel free to edit the PR directly. :slightly_smiling_face:

I agree with this.

And yeah, this is tricky.

Iā€™m not sure of the answer (also very sleep deprived today!)

1 Like

I am more or less a beginner and I was more confused about the REPL then the print() function :grinning: I just learned a lot! It may helps to extend the example like this:

# This function has an explicit return.
def add_two_numbers(number_one, number_two):
  return number_one + number_two   


# Using print() with the function call returns the sum of the numbers.
>>> print(add_two_numbers(3, 4))
7

# I could also save the return in a variable and print it afterwards.
>>> sum_with_return = add_two_numbers(5, 6)
>>> print(sum_with_return)
7

#This function does not have an explicit return. 
# That means, it does not return anything.
def add_two_numbers(number_one, number_two):
  result = number_one + number_two

#Using print() with the function call shows that 
# the function above is actually returning **None**.
# **None* means, there is really none!
>>> print(add_two_numbers(3, 4))
None

# Same if I save the return in a variable.
# Because there is no return, the variable is empty.
# **None* means, there is really none!
>>> sum_without_return = add_two_numbers(5, 6)
>>> print(sum_without_return)
None

ooh. I like the variable example, thatā€™s a nice one!

Let me get through my morning queue, and Iā€™ll massage things a bit to add that in. :smile:

2 Likes