factorial using recursive function - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: factorial using recursive function (/thread-29268.html) Pages:
1
2
|
factorial using recursive function - spalisetty06 - Aug-25-2020 Hello, I am practicing on factorial using Recursive function. I am facing an issue here. Kindly correct me. '''Recursive Function''' def factorial(n): if n == 0: result = 1 else: result = n * factorial(n - 1) print("the factorial of {} is {}".format(n, result)) return result factorial = factorial(5) Sorry, I got it, I forgot to give factorial while calling. Once added, it is resolved. I have another question please. once it comes to result = n * factorial(n - 1), isn't it that, it should go to def factorial(n): again (the first line), why is it printing print("the factorial of {} is {}".format(n, result)). I hope you understand what I am trying to ask. What I meant is, under else statement, since it is factorial(n - 1), it is supposed to go function definition, why is it printing the next statement called print ("the factorial of {} is {}".format(n, result))
RE: factorial using recursive function - buran - Aug-25-2020 your code '''Recursive Function''' def factorial(n): if n == 0: result = 1 else: result = n * factorial(n - 1) print("the factorial of {} is {}".format(n, result)) return result factorial(5), provided here works fine for me however, your traceback suggest you have following line in the code you runfactorial(n) == 1 RE: factorial using recursive function - spalisetty06 - Aug-25-2020 Hi Buran, I updated the post, it is working fine, but I have a new question, kindly look at it. RE: factorial using recursive function - buran - Aug-25-2020 your initial code was fine and working. You were running different code, with following line factorial(n) == 1 as suggested by the traceback, which is now removed from your post! your changes now are (i) BAD - with your line factorial = factorial(5) now name factorial has value of 5! and the function is no longer available to you (you overwrite the name factorial and (ii) the output show that you have more code, i.e. print(factorial) in order to produce 120 as last line of output.As to your question - first time when it will reach line 7 print("the factorial of {} is {}".format(n, result)) is wehn n==0, that is why the order is 0 to 5, not the other way around.I guess you can use this site to visualise the execution and get better understanding what's going on http://www.pythontutor.com/visualize.html#mode=edit RE: factorial using recursive function - spalisetty06 - Aug-25-2020 This might be helpful as it ha line numbers '''Recursive Function''' def factorial(n): #line 1 if n == 0: #line 2 result = 1 #line 3 else: #line 4 result = n * factorial(n - 1) #line 5 print("the factorial of {} is {}".format(n, result))#line 6 return result #line 7 factorial = factorial(5) #line 8 print(factorial) #line 9my question is, in line 5, when it is calling factorial(n - 1), isn't it supposed to go to line 1? why is it printing the statement under line 6. Isn't it how it works? If it is coming to line 6, why is it not going to line 7? RE: factorial using recursive function - buran - Aug-25-2020 you are confused, run your code here http://www.pythontutor.com/visualize.html#mode=edit you will see how the execution goes and fix the problem with factorial = factorial(5)
RE: factorial using recursive function - spalisetty06 - Aug-25-2020 (Aug-25-2020, 02:29 PM)spalisetty06 Wrote: Hi Buran, Thank You Buran for the elongated reply. Thanks again, but this code is working perfectly. def factorial(n): #line 1 if n == 0: #line 2 result = 1 #line 3 else: #line 4 result = n * factorial(n - 1) #line 5 print("the factorial of {} is {}".format(n, result))#line 6 return result #line 7 factorial = factorial(5) #line 8 print(factorial)my question is, in line 5, when it is calling factorial(n - 1), isn't it supposed to go to line 1? why is it printing the statement under line 6. Isn't it how it works? If it is coming to line 6, why is it not going to line 7? RE: factorial using recursive function - buran - Aug-25-2020 don't add line numbers as comments, it;s confusing. using python` tags will show line numbers RE: factorial using recursive function - buran - Aug-25-2020 (Aug-25-2020, 02:45 PM)spalisetty06 Wrote: Thank You Buran for the elongated reply. Thanks again, but this code is working perfectly. no, it's not. the function is OK (and was OK - you were running different code!). The problem is line 8. try def factorial(n): if n == 0: result = 1 else: result = n * factorial(n - 1) print("the factorial of {} is {}".format(n, result)) return result factorial = factorial(5) print(factorial) # NOW calculate factorial of let's say 4 print(factorial(4))the result
RE: factorial using recursive function - buran - Aug-25-2020 (Aug-25-2020, 02:45 PM)spalisetty06 Wrote: my question is, in line 5, when it is calling factorial(n - 1), isn't it supposed to go to line 1? why is it printing the statement under line 6. Isn't it how it works? If it is coming to line 6, why is it not going to line 7?How many times should I tell you to run the code on the site I posted twice in order to SEE how the execution goes? |