Python Forum
factorial using recursive function
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
factorial using recursive function
#11
(Aug-25-2020, 02:50 PM)buran Wrote:
(Aug-25-2020, 02:45 PM)spalisetty06 Wrote: Thank You Buran for the elongated reply. Thanks again, but this code is working perfectly.
Wall Wall Wall Wall Wall Wall Wall Wall Wall Wall
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

Error:
the factorial of 0 is 1 the factorial of 1 is 1 the factorial of 2 is 2 the factorial of 3 is 6 the factorial of 4 is 24 the factorial of 5 is 120 120 Traceback (most recent call last): File "/home/boyan/sandbox2/forum.py", line 11, in <module> print(factorial(4)) TypeError: 'int' object is not callable


What is wrong in that? I am calling the function with n value as 5. Because, the function is returning a value, I used
factorial = factorial(5)
print(factorial)
even as you did, directly
print(factorial(4))

is fine too.
Reply
#12
when you do
factorial = factorial(5)
factorial is not the function anymore, it's an int value 120. So you can not use the function factorial any longer. That's why the example I show you raise type error!
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#13
Thank You, I am not sure, who developed this visualizer, he should be awarded a million dollar
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  with open context inside of a recursive function billykid999 1 549 May-23-2023, 02:37 AM
Last Post: deanhystad
  Why recursive function consumes more of processing time than loops? M83Linux 9 4,128 May-20-2021, 01:52 PM
Last Post: DeaD_EyE
  Combine Two Recursive Functions To Create One Recursive Selection Sort Function Jeremy7 12 7,195 Jan-17-2021, 03:02 AM
Last Post: Jeremy7
  Execution of Another Recursive Function muzikman 5 2,947 Dec-04-2020, 08:13 PM
Last Post: snippsat
  Don't Understand Recursive Function muzikman 9 3,576 Dec-03-2020, 05:10 PM
Last Post: muzikman
  list call problem in generator function using iteration and recursive calls postta 1 1,863 Oct-24-2020, 09:33 PM
Last Post: bowlofred
  Comparing recursion and loops using two scripts (basic factorial) Drone4four 3 2,202 Oct-11-2020, 06:48 PM
Last Post: deanhystad
  Python factorial code timebrahimy 4 58,729 Sep-27-2020, 12:23 AM
Last Post: timebrahimy
  Recursive function returns None, when True is expected akar 0 3,353 Sep-07-2020, 07:58 PM
Last Post: akar
  factorial, repeating Aldiyar 4 2,738 Sep-01-2020, 05:22 PM
Last Post: DPaul

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020