Aug-25-2020, 03:01 PM
(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.![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
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.