-3
  • Hello, I'm extremely new to Python and wanted to try out the newly taught functions, and cobble up this:
def print_thrice(a):
    print(a)
    print(a)
    print(a)
def prt(a,b):
    print_thrice(a)
    print(b) 
print_thrice(prt("Name:","Age:"))

I expected it to print out Name: Name: Name: Age: (with linebreaks of course) 9 times, but what I got turns to be

Name:
Name:
Name:
Age:
None
None
None

What I had in mind was to have 9 of Name: Name: Name: Age:, so I tried to print_thrice the already printed thrice Name:along withAge: in prt("Name:","Age:"). The first 3 evidently appeared, which I guess means that the first batch of print_thrice worked, but I have no idea why I'd be getting three Nones afterwards.

Eventually I gave up and just did 3 lines of prt("Name:","Age:") instead of print_thrice(prt("Name:","Age:")), and it seemed to work fine. Can anyone please explain what I did wrong?

  • 4
    The return value of prt is None since it returns nothing. This is why you see None being printed. See here: https://stackoverflow.com/q/15300550/5079316 – Olivier Melançon Jan 20 '21 at 17:03
  • you just need to call `prt("Name:", "Age:"))` – Giovanni Rescia Jan 20 '21 at 17:05
  • What exactly are you trying to do? prt() returns None, that’s why you get 3 Nones after your desire output. – Ori David Jan 20 '21 at 17:05
  • Others have already answered your question. I just want to welcome you to the SO. Please do not get discouraged by the negative score. – C. Pappy Jan 20 '21 at 17:11
  • I guess what I had in mind was to have 9 of `Name: Name: Name: Age:`, so I tried to `print_thrice` the already printed thrice `Name:` along with `Age:` in `prt("Name:","Age:")`. The first 3 evidently appeared, which I guess means that the first batch of `print_thrice` worked, but I have no idea why I'd be getting three Nones afterwards – Dixie Normus Jan 20 '21 at 17:24

1 Answers1

0

Function is a callable object in Python and it has a return value. If you explicitly did not ask something to return by the function, it returns None by default.   I presume this may be what you were trying to do; If not, please leave a comment.

def print_thrice(argumentOne, argumentTwo):

    return '\n'.join(f'I am {argumentOne}  and I am {argumentTwo} years old' for _ in range(3))

print(print_thrice("George","25")) # caling the function print_thrice



I am George  and I am 25 years old
I am George  and I am 25 years old
I am George  and I am 25 years old
nectarBee
  • 401
  • 3
  • 9