5. Factorial



Would someone please kindly help me understand why the following code isn’t working?

It outputs 4 instead of 24

def factorial(x):
	total = x
	turn = x
	for item in range(x-1):	
		turn -=  1
   	        total *= turn
	return total

print factorial(4)


I’ve tried another way based on a hint from another post, it seems to work fine

def factorial(x):
total = 1
for item in range(1, x+1):
total = total * item
return total

print factorial(4)

So I’m just wondering why the above code (my initial attempted version) doesn’t also work

Thanks a bunch :slight_smile:


your original code seems to be doing fine:

backup link

i re-indented the code, given the codecademy editor messed that up


ok thanks
yes it worked last time I was on that exercise, seems to be glitchy??
Although my WIFI isn’t the best at the moment

Glad my code works though! cheers :slight_smile:


well, your code is fine so you seem to understand it :slight_smile:

Now you can progress to the next exercise :slight_smile:


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