Factorial


#1

pls kindly help, I'm stuck in this code

https://www.codecademy.com/courses/python-intermediate-en-rCQKw/0/5?curriculum_id=4f89dab3d788890003000096

def factorial(x):
    if x < 2:
        return 1
    total = 0
    count = x
    n = 0
    while x != 0 and x !=1:
        n += 1
        count-=1
        total = x * x-n
    if count == 1:
        return total
    else:
        n += 1
        total = total * x-n
        count -= 1
    if count == 1:
        return total

#2

Two tips,

Your while loop can be simplified if all you check for is if x >1, just make sure to set it equal to the abs function,

x = abs(x)
fact=x #use in while loop 
while x > 1

Last tip.

Inside your while loop you can generate the factorial by setting x = x -1, and then multiplying it by f,

n -= 1
f *= n

#3

in doing so all you wll need to do is return fact and your function should work. :slight_smile:


#4

what is n, why n-=1?


#5

I believe he meant x.


#6

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