You can display the final result returned by your recursive function in two ways within a sub function:
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n-1)
def calculate_factorial(n):
result = factorial(n)
print("The factorial of ", n, " is: ", result)
calculate_factorial(5)
Output:
The factorial of 5 is: 120
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n-1)
def calculate_factorial(n):
result = factorial(n)
return result
final_result = calculate_factorial(5)
print("The factorial of 5 is: ", final_result)
Output:
The factorial of 5 is: 120
Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss
Asked: 2023-06-07 10:49:45 +0000
Seen: 20 times
Last updated: Jun 07 '23
Despite setting `verbose` to `False`, why does Moviepy still display a progress bar?
How can I display a loading message in aggrid using react js?
How can Xcode 13.1 display the source code framework of an app?
How can I display the address field from the lead object in various sections?
Can AG-GRID display everything in a single column?
How can I modify Angular 8 to display a blank page without any error messages?
How can you display a Pandas Dataframe using a for loop?
How can the output of VBA be displayed in a cell within Excel?