-1
def greeter(name):
    print('Hey %s, I was going to call you 
          yesterday.'%name)

    print('Damn %s, you grew so much since HS.'%name)

    print('It was nice to see you %s!\n'%name)

The function is meant to grab a name and insert it in these greetings.

Input:

    friends = ['kevin','Darwin','Erica']

        for friend in friends:
         print(greeter(friend))

Ouput:

Hey kevin, I was going to call you yesterday. Damn kevin, you grew so much since HS. It was nice to see you kevin!

None

Hey Darwin, I was going to call you yesterday. Damn Darwin, you grew so much since HS. It was nice to see you Darwin!

None

Hey Erica, I was going to call you yesterday. Damn Erica, you grew so much since HS. It was nice to see you Erica!

None

Question: Why is it that every time the function is executed on a friend in the for loop afterwards 'None' pops up?

  • 3
    `greeter` does not return a value, so `print(greeter(...))` has nothing to print. – 001 Mar 06 '19 at 19:39
  • 2
    Possible duplicate of [Function returns None without return statement](https://stackoverflow.com/questions/7053652/function-returns-none-without-return-statement) –  Mar 06 '19 at 19:40
  • Because you print the returned value of the function, which is by default a `None` value (unless specified) – mr_mo Mar 06 '19 at 19:40

1 Answers1

5

In the for loop, you're printing the return value of the greeter function. It doesn't return anything, so None is displayed. You can change your loop to just call the function, since greeter prints all the output itself.

for friend in friends:
    greeter(friend)
Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880