Function to find the factors of a negative number
<p>I need to write a function that will find the factors for a negative number and output them to a list. How would I do that? I can get my function to do positive numbers (see below) but not negative ones.</p>

<pre><code>#Finds factors for A and C
def factorspos(x):
    factorspos = [1,-6];
    print("The factors of",x,"are:")
    for i in range(1, x + 1):
        if x % i == 0:
            factorspos.append(i)
            print(i)
</code></pre>

<p>I tried changing the values that the loop counts from so it would count from the number chosen to 1 (Code below) but still yielded no results :(</p>

<pre><code>#Finds factors for A and C
def factorspos(x):
    factorspos = [int(-6),1];
    print("The factors of",x,"are:")
    for i in range(int(-6), x + 1):
        if x % i == 0:
            factorspos.append(i)
            print(i)
</code></pre>

<p>I have changed Cco to a fixed number.</p>

<pre><code>#Finds factors for A and C
def factorspos(x):
    Cco = -6
    factorspos = [int(Cco),1];
    print("The factors of",x,"are:")
    for i in range(int(Cco), x + 1):
        if x % i == 0:
            factorspos.append(i)
            print(i)
            return factorspos
</code></pre>

<pre><code>def factorspos(x):
    x = int(x)
    factorspos = []
    print("The factors of",x,"are:")
    if x &gt; 0: # if input is postive
        for i in range(1,x+1):
            if x % i == 0:
                factorspos.append(i)
                print(i)
        return factorspos
    elif x &lt; 0: #if input is negative
        for i in range(x,0):
            if x % i == 0:
                factorspos.append(i)
                print(i)
        return factorspos


print(factorspos(12))   #outputs [1, 2, 3, 4, 6, 12]
print(factorspos(-12))  #outputs [-12, -6, -4, -3, -2, -1]
</code></pre>

<p>You were actually really close to fixing your issue. I took the liberty of adding an extra function to what you had. Basically I added a condiction checker to see if the input <code>x</code> is positive or negative, the the function did two different things. What they do was what you provided, but cleaned up. </p>

<p>Things to note <code>range()</code> starts from one the first number inclusive, and ends one number short of the second parameter. <code>range(1,10)</code> will give you 1 to 9. So that's why if you look, the negative section the range goes from x to 0 since that will say x to -1. In the positive section it will go from 1 to x+1 since +1 insures we include our input. The rest you know about since, well you wrote it; if not feel free to ask questions. </p>

python python-3.x