Python :15.5 Practice Makes Perfect(Factorial)


Hello,Can anybody look at my code

I get following error message

Oops, try again. factorial(4) returned 12 instead of 24

def factorial(x):
    if x==1:
        return 1
        return (x)*((x)-1)

print factorial(4)


The apparent intended method was a recursive function, which, however, this is not. As factorial goes, a recursion looks elegant but is in fact not the approach to reach for. I'm sure the jury is out on that one. Iteration is the way to go. Work to develop an iterative function that returns a factorial.

The recursion you were after looks like this (to which you were very close)

>>> def factorial(x):
    return 1 if x == 1 else x * factorial(x - 1)

>>> factorial(7)

27! is a huge number...

>>> factorial(27)

That's 28 magnitudes of 10, 5 magnitudes greater than the distance light has travelled since the start of the of known universe.

>>> 1.38e10 * 1e13
1.38e+23            # distance in kilometers of 13.8 billion light years

For the record, even Python does not use recursion in the math.factorial function.

for (i=1 ; i<=x ; i++) {    // C

On this page, F3 math_factorial.


If you go by your function definition, print factorial(4) prints 12, instead of 24. I’ll tell you why.

    return (x)*((x)-1)

What that is doing is multiply just x by (x - 1) i.e., 4 * 3 and return the product.

What you would want it to do is return the product of 4 * 3 * 2 * 1.

To achieve that, use either a loop or recursion.

By the way, 0! is 1, too.


Thanks For your inputs. I solved the problem using recursion method. But I couldn't solve it by looping. As far as I know we need to to loop using range(x) method. But then I got lost.Can you provide Hint?


OK. Initialize a result variable to 1.

If the function parameter x is 0, return 1.

Else, go for a range of 1 to x + 1, multiplying the result by each number in the range and iteratively assigning the product to the result.

Let me know if you still can't get it.


This is my code:

def factorial(x):
    fact=1## why assign to 1 instead of zero??
    if x==0:
        return 1
        for i in range (1,x+1):
        return fact
print factorial(4)

Thank you very much for your time and counsel


Glad I could help :slight_smile:

Assign zero and see what it does.


Oh now I understand ! Assigning fact to zero will render everything zero


If you are not busy could you care to take a look at my code for

def is_prime(x):
    if x>2:
        for n in range(2,x-1):
            if x%n==0:
                return False
                return True
    elif x==2:
        return True
        return False
print is_prime(2)
print is_prime(3)
print is_prime(4)

I get following output


I am sorry if I am annoying you or wasting your time or disturbing your sleep If you are in Asia


Change if x>2: to if x > 1:

Then, loop range should be (2, int(x**0.5)+1). Hope you understand why I'm taking the root of x and converting it to an int.
* If x is divisible by n with a remainder of zero, x is not prime.
* Else, x is prime.


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.