Я хотел бы создать новую функцию, которая имеет два входа (n, input_function), которая возвращает новую функцию output_function, которая выполняет функцию input_function, но она делает это n раз. Вот изображение того, чего я пытаюсь достичь
def repeat_function(n, function, input_number):
for i in range(n):
input_number = function(input_number)
return input_number
def times_three(x):
return x * 3
print(repeat_function(3, times_three, 10)) #prints 270 so it's correct
print(times_three(times_three(times_three(10)))) #prints 270 so it's correct
#This function does not work
def new_repeat_function(n, function):
result = lambda x : function(x)
for i in range(n-1):
result = lambda x : function(result(x))
return result
new_function = new_repeat_function(3, times_three)
#I want new_function = lambda x : times_three(times_three(times_three(x)))
print(new_function(10)) # should return 270 but does not work
Я старался изо всех сил, чтобы реализовать это, но это не работает. Мне нужна функция new_repeat_function, чтобы делать то, что делает repeat_function, но вместо возврата и целочисленного ответа, как это делает repeat_function, функция new_repeat_function должна возвращать time_three () n раз.