Help with recursive function in Python

Asked

Viewed 76 times

0

I am a beginner with a lot of difficulty in understanding the recursive function, I know it is a function that calls itself, but I can not make it work happily, in the exercise practiced in class, was given the exercise...

"Make a function that receives a number and calculate its factorial."

What I did was the function below...

def fatorial():
              num = int(input('Insira um número: '))
              contador = 0
              fatorial = 1
              while contador < num:
                            contador += 1
                            fatorial = fatorial * contador
              print('O fatorial de', num, 'é', fatorial)
fatorial()

The function is working smoothly, now I need to understand how to do the recursive function in this same exercise done previously, where it asks...

"Repeat the previous exercise using recursion, that is, a function that calls itself, remembering that 3! = 32!, what 2! = 21!, that 1! = 1*0! and that 0! = 1."

It would be the function considered recursive below ?

def fatorial(x):

              if x == 1:
                            return 1
              elif x==0:
                            return 1
              else:
                            return x * fatorial(x-1)
while True:
              x = int(input("Fatorial de: "))
              print("Fatorial do número escolhido é: ",fatorial(x) )
              fatorial(x)

I would also like to know if, the recursive function should have 1 single function or can have 2 functions, as well as I would make insertions in the first scope for both work if it is the case or if I should use only 1 scope, if it would be the second most indicated ?

Who can help me with the doubt, thank you !

  • Yes, the function fatorial is recursive because it calls itself (I suggest you read all the links that are in the blue box of this question). "the recursive function must have 1 single function or it can have 2 functions, as well as I would make insertions in the first scope for both work if it is the case or if I should use only 1 scope, if it would be the second most indicated" - I don’t understand what you mean, maybe you’re using the wrong terms to describe certain things or situations, because you got confused.

  • Thank you...I probably made a mistake even being a mere beginner in python. Do you think the last time I programmed something, it was in the time of the extinct basic...rs

1 answer

0

Hello, the recursive version of the factorial function looks good to me, just a hint: the case of x==1 and x==0 is the same (Return 1) so you don’t need to separate these cases into two different ifs:

def fatorial(x):
    if x<2:
        return 1
    else:
        return x * fatorial(x-1)

and yes, if necessary a recursive function can call other functions

  • Thank you...I actually put Elif, because when typing 0 (zero) and fetching its factorial (1), the instruction ended. This way I left her operating endlessly for inserting numbers during the exercise.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.