Can anyone tell me why? Still confused


#1




My first code was like this:

def factorial(n):
    while n > -1:
        
        if n > 1: 
            return n*(n-1)
          
        elif n==1:
            return 1
        elif n==0:
            return 1
        else:
            print"something wrong with your code!"


It was only multiplied once : n * (n-1).
But when I changed n*(n-1) to n* factorial (n-1), it worked.
Could anybody tell me why? Thanks !


Replace this line with your code.


#2

in the first case, the function ended because a return keyword was reached.

However, in the second case, you something called recursion or recursive function calls. Very briefly, this is the function calling itself. Until the base case is reached

there is a really good topic about this on the forum:


#3

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