HELP, this function is not working properly


hello, i am new to python. Can you please explain to me why this function is not working?
the output is 12 when it is suppose to be 24

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

after me looking at solutions for this particular factorial problem, how come this function work:

def factorial(num):
  factorial = 1
  for item in range(1,num + 1):
    factorial = factorial  * item 
  return factorial

as opposed to the above one.


Compare these two lines closely…

factorial = num * i

factorial = factorial  * item

See the difference? Which one replaces the value stored, and which one augments it?


Hello. Thanks for helping. But I must ask, what does augments mean? I’m sorry for my ignorance.


To augment something is to increase it. That might be by addition (of a positive number) or multiplication (provided the factor is greater than 1). Either one will increase a value.


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