Python Forum

Full Version: minor mistake in code for factorial
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello,
I believe, I have written it correctly, but I am missing something.
'''write a program to get the factorial of a given number'''
def fact(number):
    result = 1
    while number > 1:
        result = result * number
        number = number -1
        return result
print(fact(5))
Output:
5
Remember that when a return statement is encountered, the function immediately returns. So, only one iteration of your while loop is performed, because the function returns at the end of that.
(Aug-22-2020, 04:51 PM)ndc85430 Wrote: [ -> ]Remember that when a return statement is encountered, the function immediately returns. So, only one iteration of your while loop is performed, because the function returns at the end of that.

I am so stupid after all, Thank you so much