-7

Beginner here, whenever I try to run the code with sum as 20 for example, it only returns [2,10], rather than the intended [2,10,4,5,5,4,10,2]

def Factors(num):
    factor1=2
    factors=[]
    while factor1<num:
        while num%factor1 == 0:
            factors.append(factor1)
            factors.append(num/factor1)
            factor1+=1
            continue
        if num%factor1 != 0:
            factor1+=1
        
        return factors
Steve
  • 7
  • 2
  • 3
    The `return` is inside the `while` loop, so at the end of the 1st iteration (`factor1` is 2), it immediately returns and the function ends. You can see this if you check the execution of your code line-by-line. Unindent your `return` to be in the same level as the outer `while`. – Gino Mempin Feb 26 '22 at 03:36
  • Try a debugger? – Abhijit Sarkar Feb 26 '22 at 03:38

1 Answers1

0

I rewrote some of this code and this is what I came up with.

factors = []

def Factor(num):
    first_factor = 1
    while first_factor <= num:
        if num % first_factor == 0:
            factors.append(first_factor)
            first_factor += 1
        else:
            first_factor += 1

    return(factors)

print(Factor(20))