5/15 Factorial




I don't seem to be getting the result I wish for and i cannot work out why the result is not a factorial.
What am i missing?

def factorial(x):
    result = 1
    if x > 0 and x == int:
        while x > 0:
            n = x - 1
            total = x * n
            x - 1
    return result


For our purposes here we are safe to assume that x is a positive integer so do not need to validate it. Validating an integer is a story in itself that needs to be convered at some point in our learning, so do follow up with this.

def factorial(x):
    result = 1
    if x > 0:
        while x > 0:
            result *= x    # tweak on this line
            x -= 1         # tweak on this line
    return result

Say that x == 7.

1 * 7
7 * 6
42 * 5
210 * 4
840 * 3
2520 * 2
5040 * 1


Thank you for your swift response.


A post was split to a new topic: Got no idea why it works


Here's what I did:

Had to go back a few lessons though to remember the import syntax :slightly_smiling:


Did you at least attempt to do this without using the built-in module?


I tried the method you described, I was close but not this concise and it was giving me an error stating to check for infinite loops.

But this made a lot of sense to set result to the initial number using x * 1, then subtracting and multiplying from there.



you could use recursion...

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


I got the syntax using the while loop :slight_smile:Thanks mtf
I just tried the import to have less line of code


So long as you are confident you can do this procedurally, step by step. It stands to reason that we would use the built in functions whenever we can from the standpoint of performace. Python modules are compiled C (I believe), so are blazing fast, and there is less of our own code to compile at run time.


:joy: it's the most efficient method.


There r many methods, below is mine:


Best answer yet..!!:joy:

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

def fac(x):
	return 1 if x < 2 else x*fac(x-1)


@aduku less lines

def fac(x):
	return 1 if x < 2 else x*fac(x-1)


Smart Answer!!:cactus:


def factorial(x):
if x <= 1:
return 1
counter = x-1
while counter > 0:
x= x * counter
counter -= 1
return x
def factorial(x):
if x <= 1:
return 1
return x * factorial(x-1)
iteration takes less Program memory but recursion is easier to code


hi mtf, i just started the codecademy course.
could you explain why you didnt use a while loop instead of the 'if x > 0'?

im new to this, and i wrote the code with 'while' instead of the 'if'. it still works but im just wondering if its bad code to write it as i have.


That was based upon the opening post. Technically, the if x > 0 line is not needed since the while condition covers that.

f = 1
while x > 0:
    f *= x
    x -= 1
return f

It's not pretty but it always returns a number no matter what the input. 1 if x is 1 or less (even negative) else x!.

For our purposes we treat it as though the inputs are always valid. Error checking will come up later on.