Why python is not able find the factorial of such numbers?

def factorial(x):
    if x < 0:
        print("not a valid num")
    elif x == 0:
        print("factorial of 0 is 1")
        for i in range(1, x):
            x = x * i



you change the value of x, this means your loop ends to early (given the factorial is bigger then the number itself, your loop ends to early)

add some print statement to see the problem in action


i didnt get you?where should i add the print statement


well, i said the value of x is increasing to rapid which will cause your loop to end too soon, so a print statement in the for loop to display the value of x. Ideally, you would figure out where to insert the print statement to better see and understand your program

you use x for both the loop condition and to store the factorial result, this is conflicting


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