One thing that I miss in these exercises is seeing how others might have done the same thing more efficiently, or even just differently. I've enjoyed seeing others solutions on this board.
My first solution was to create a list of the factorials first, then iterate through that list to multiply them all together (this did pass btw):
def factorial(x): num_list =  while x-1 > 1: num_list.append(x) x = x-1 else: num_list.append(x) total = 1 for i in num_list: total *= i return total
This worked, but then I re-read the instructions:
"To calculate the factorial of a non-negative integer x, just multiply all the integers from 1 through x."
That sounded like:
Or something close to that (actually x+1 because range stop is NOT inclusive). This definitely sounded like it would be simpler. So I re-coded using range and got this:
def factorial(x): total = 1 for i in range(1,x+1): total *= i return total
Obviously this doesn't play nice with zero or negative integers. So I put in a quick check:
def factorial(x): if x <= 0: return None else: total = 1 for i in range(1,x+1): total *= i return total
This now returns "None" for any integer below 1. I couldn't figure out a way to just make the algorithm work if it took zero as an input. And since the exercise called for "non-negative integers," that to me includes zero.
How did you solve it?