# Just need a simple explanation

#1
``````def factorial(x):
total = 1
while x>0:
total *= x
x-=1
``````

can someone explain me this bit of code

#3
``````
factorial(4)          # I will use 4 as my x input;

def factorial(x):     # Defined a function with x as an input;

total = 1         # You start your total sum variable with 1 unlike previous
# lessons using 0 because we are dealing with multiplication.
# Otherwise   total(0) * x(4) = 0

while x>0:        # While (4) > 0: since 4 is greater then 0 continue loop

total *= x    # total = 1 * 4

x-=1          # now take your x input(4) subract it by 1 which will = (3) as your new x and restart the loop
# From this point we got back to the while loop
# is 3 > 0? yes it is so continue loop

# dont forget your total is still (4) so
# new Total = 4 * 3 which gets you 12
# back to your input which was 3 from last loop so now 3 - 1 = new input 2
# back to while loop 2>0? yup so continue ect

# the process gets you to 4*3*2*1 which is the factorial of 4 factorial(4) = 24

return total      # so now that we finished the loop consideringing 0 is not > 0 it returns 24

def factorial(4):
total = 1
while x>0:
total *= x
x-=1

``````

#4

THANK YOU SO MUCH! Made it pretty clear, wish it showed explanations like this in the end or something

#5

#6

#7

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.