#5 factorial


#1



I Dunno why it doesnt work, error says- factorial(2) return 1 , instead of 2


def factorial(x):
    while x!=0:
        if x!=1:
            k=x*(x-1)
            x-=1
        else :
            print 1
        return x
        print k


#2

add a function to call to see what is happening:

def factorial(x):
    while x!=0:
        print "looping"
        if x!=1:
            k=x*(x-1)
            x-=1
        else :
            print 1
        return x
        print k
    
print factorial(5)

oops, you only loop once. What is happening? A function ends the moment a return keyword is reached, this is currently happening in the first iteration of your loop.


#3

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