This is my first time around, so i would appreciate your patience with what might appear as a lame looking question 🙂
I’m trying to write a function called do_n that takes a function object and a number, n, as arguments and then call the given function n times. Here’s the code:
def name():
print 'Jack'
def do_n(fo, x):
if x <= 0:
return
print fo
(fo, x-1)
When making a function call from within main:
do_n(name, 3)
I get the following outcome:
<function name at 0x01F93AF0>
I’m trying to get the program to print out:
Jack
Jack
Jack
Many thanks in advance
You are neither calling the function, nor are you actually doing the recursive call. Corrected version:
To call a function
ntimes, you’d usually use a for loop instead of tail recursion in Python: