Python Forum

Full Version: Factorial sketch(python 3)
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am a fairly new python student, so please excuse any errors.
I am having trouble coming up with the correct output for this sketch i got from a friend. Could you please point out what's wrong and how to correct it?
For example, if i enter Factorial(16), i get 16 as the output.
But if i enter Factorial(4) i get 7 as the output.
This is the sketch:


n=7
def factorial(n):
    if n<0:
        n*=-1
        if (n==0):
            return 0
        f= 1
        while n>1:
            f*=n
            n-=1
            return f
That indentation is not the indentation your are using. If it was, factorial(16) would return None. Please make sure the indentation in your post matches that in your code. Indentation is important in Python, and I suspect that might be where the error is.