python 将数字扩展为质因数的程序

python program to expand a number in to prime factors

我的代码有问题

请做得更好


def factors (num):
    #num =  int( eval(input ("enter a number : ") ))

    to = (num//2)+2
    dict = []

    for a in range(2,to):
        for b in range (2,a+1):
            if num % b == 0:
                dict.append(b)
                num = num // b

    print (dict)

factors(2*2*2*2*2*2*2*2*2*2*2*2*2)

想要的结果是

[2,2,2,2,2,2,2,2,2,2,2]

但我得到了这样的东西

[2, 2, 2, 4, 2, 4, 2, 4, 2, 2]
def factors(num):
    d = []
    while num > 1:
        for a in range(2, num+1):
            if num % a == 0:
                d.append(a)
                num = num // a
                break
    print(d)


factors(2 ** 14) # [2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2]