title: Problems of defining a factorial function in python
body: <p>I'm recently starting to learn coding in Python, this is the code that I tried to define a function that helps me to find the factorial of a number. Yet, this chunk of code always returns me the double of its factorial.</p>
  <pre><code>def factorial(x):
     empty = None
     try:
        adj_x = int(x)
     except:
        print(&quot;Invalid Input&quot;)
     if adj_x &lt; 0:
        print(&quot;Invalid Input&quot;)
     elif adj_x == 0:
        print(0)
     else:
        l_adj_x = adj_x - 1 # if input == 4, this is 3
        r = range(1, l_adj_x) # 1, 2, 3
        for k in r:
           n = adj_x - k # 4-1 = 3, 4-2 = 2, 4-3 = 1
           if empty is None:
              empty = n
           else:
              empty = empty * n
           n_adj_x = adj_x * empty * n
        print(n_adj_x)
  </code></pre>
  <p>I realized that it is the problem of the <code>n</code> in this line:</p>
  <pre><code>n_adj_x = adj_x * empty * n
  </code></pre>
  <p>Based on my own understanding, the last <code>n</code> in the for-loop should be 1 (I took 4 as an example of my input and stated all the possible outcomes of the loop in the comments next to each line of code) , if so, why does it appear to be the double of the correct answer (when I include <code>n</code> in <code>n_adj_x = adj_x * empty * n</code>) since the <code>n</code> should equal to 1?</p>