Search code examples
pythonfactorial

how to solve this factorial


I've managed to get factors in a list but I can't finish because I'm doing something wrong in the end, the count variable is not updating with the products. I want to solve it without using the factorial function.

Question: Write a program which can compute the factorial of a given number. The results should be printed in a comma-separated sequence on a single line. Suppose the following input is supplied to the program: 8 Then, the output should be: 40320

Hints: In case of input data being supplied to the question, it should be assumed to be a console input.

user = int(input("Input a number: "))
factors = []


while user > 0:
    n = user * (user - 1)
    if user == 1:
        factors.append(1)
        break
    else:
        factors.append(user)
        user -= 1

count = 0   # this should be updating but it's not 
for f in range(factors[0]):  # I'm not sure if this is the correct range
    counting = factors[f] * factors[f + 1] # I'm not sure about this either
    count = count + counting 
    f = f + 1 

Solution

  • Just change the last part of your program to:

    result = 1
    for f in factors:
        result *= f
    
    print result