📜  负数因子python代码示例

📅  最后修改于: 2022-03-11 14:46:19.292000             🧑  作者: Mango

代码示例1
def factorspos(x):
    x = int(x)
    factorspos = []
    print("The factors of",x,"are:")
    if x > 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 < 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]