5. factorial


I was wondering the error with my code

def factorial(x):
    num = 1
    while x > 0:
      num = num + 1
      x = x + 1
    return num


By default, a function returns None at the end of the function. If we want to return something else at the end of the function, we can use the return keyword.

Given return is the last thing a function does, when a return keyword is reached, the function ends

so currently, your function ends in the first iteration of your loop

You also have an infinity loop once you move return outside the loop. x is only increasing, so x > 0 will never become false.

factorial 3 is 6 ( 3 * 2 * 1), not sure why you use num + 1 given we need to multiply


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