Python Recursive Function Factory Value

Asked 2 weeks ago, Updated 2 weeks ago, 3 views

def fact(n):
    if n==1:
        Call print('fact('n',) function! ')
        return 1,print ('fact(',n',)value ',1,'return')
    else:
        print('fact('n',) function call!! ')
        return n*fact(n-1), print('fact('',n',)value return')

def ckeck(n):
    if n==1:
        return 1
    else:
        return n*ckeck(n-1)

a=int (input('Enter an integer:')
fact(a)

print(the factory value of 'a' is 'ckeck(a),')

I make the chords like this.

fact(3) function call!! 
fact(2) function call!! 
fact(1) function call! 
Return fact (1) value 1
Return fact (2) value 2
Return fact (3) value 6
The factory value of 3 is 6

I want to print it out like this. Help me. I just don't know what to put in between the price returns. Masters, please help me.

python

2022-09-20 08:47

1 Answers

def fact(n):
    if n==1:
        Call print('fact('n',) function! ')
        r = 1
        print('fact(',n',)value ',r',return')
        return r
    else:
        print('fact('n',) function call!! ')
        r = n*fact(n-1)
        print('fact(',n',)value ',r',return')
        return r

def ckeck(n):
    if n==1:
        return 1
    else:
        return n*ckeck(n-1)

#a=int (input('Enter an integer:')
a = 3
fact(a)

print(the factory value of 'a' is 'ckeck(a),')

Method using decorator.

def fact_decorator(f):
    def fact_dec(n):
        print(f"fact({n}) function call!")
        r = f(n)
        print(f"fact({n}) value {r} return")
        return r

    return fact_dec


@fact_decorator
def fact(n):
    if n == 1:
        return 1
    else:
        return n * fact(n - 1)


# a=int (input('Enter an integer:')
a = 3
r = fact(a)

print(the factory value of a, " is", r, "")


2022-09-20 08:47

If you have any answers or tips


© 2022 pinfo. All rights reserved.