-2

Problem: Playing with the idea of exchanging variables between functions.

I am executing the following code:

def benefits():
    list =  ["Beautiful", "Explicit", "Simple", "Readability","Easy to share"]
    return list

def statement(benefit):
    print("The benifit is " + benefit )          


def benefits_of_functions():  
    benefits_list = benefits()
    for benefit in benefits_list:
        print(statement(benefit))


benefits_of_functions()

I get the error:

The benifit is Beautiful
None
The benifit is Explicit
None
The benifit is Simple
None
The benifit is Readability
None
The benifit is Easy to share
None

I cannot understand the "none". Could you please help me figure out why that's in the output?

Schneider
  • 37
  • 1
  • 8
  • because your `statement` function simply prints something, and doesn't return anything, so it implicitely returns `None`. Inside your `benefits_of_functions` function, you loop over your list, then print the result of passing the items in the list to `statement`, which prints your messeage, but you are printing the result of that, which is `None`. `def statement` should probably just `return "The benifit is " + benefit` instead of `print`ing it. – juanpa.arrivillaga Feb 05 '20 at 19:27
  • Note, it is important to understand, you don't pass *variables* into functions, you pass *objects*. – juanpa.arrivillaga Feb 05 '20 at 19:27

1 Answers1

2

Return instead of print in the statement function:

def benefits():
    list =  ["Beautiful", "Explicit", "Simple", "Readability","Easy to share"]
    return list

def statement(benefit):
    return "The benifit is " + benefit


def benefits_of_functions():  
    benefits_list = benefits()
    for benefit in benefits_list:
        print(statement(benefit))


benefits_of_functions()
JacobIRR
  • 8,545
  • 8
  • 39
  • 68