Why you do dis (factorial)


#1

I was just wondering If my code is good

so I spent maybe an 45 mins on this question lol much stupid.
my initial thought was to arrive at the code at the bottom.

but i gave up because i kept using the "num" INSIDE the loop. which means the num value actually resets each time the loop is done.
I arrived at this slightly long winded way instead. after scratching my head for a while.


code 2

def factorial(x): 
    multiply = []
    b = 1
    if x == 0 or x == 1:
        return 1
    else: 
        multiply.append(x)
        while x >= 2 :
            y = x - 1
            multiply.append(y)
            x -= 1
        else:
            for a in multiply:
                b *= a
    return b


what I wanted.
def factorial(x):
    num = 1
    while x >=1:
        num = num * x
        x -= 1
    return num


#2

I would do it using a range, as so:

def factorial(x):
    num = 1
    for i in range(1, x):
        num *= i
    return num

#3


def factorial(x):
        num = 1
        for i in range(1, x):
            num *= i
        return num

it works but I thought it would multiply all numbers apart from the input.
since it would be 1, 2, 3, 4. sigh