Find factorial of a list of numbers

Viewed 14785

I have a set of numbers:

list = {1, 2, 3, 4, 5}

I wish to create a function that calculates the factorial of each number in the set and prints it.

input_set = {1, 2, 3, 4, 5}
fact = 1
for item in input_set:
    for number in range(1,item+1):
        fact = fact * number
    print ("Factorial of", item, "is", fact)

The output I am getting is:

Factorial of 1 is 1
Factorial of 2 is 2
Factorial of 3 is 12
Factorial of 4 is 288
Factorial of 5 is 34560

Which is obviously wrong. I would really like to know what is wrong with my code and how to fix it.

Note: I don't wish to use the math.factorial function for this code.

8 Answers

set fact=1 inside for loop.

input_set = {1, 2, 3, 4, 5}
for item in input_set:
    fact = 1
    for number in range(1,item+1):
        fact = fact * number
        print ("Factorial of", input, "is", fact)
def factorial(n):

    fact = 1

    for factor in range(1, n + 1):
        fact *= factor

    return fact

>>> my_list = [1, 2, 3, 4, 5]
>>> my_factorials = [factorial(x) for x in my_list]
>>> my_factorials
[1, 2, 6, 24, 120]

You need to reset fact before the second for loop, it's just multiplying with the result of the previous factorial.

There is also a built-in factorial() in math module.

from math import factorial

def factorialize(nums):
    """ Return factorials of a list of numbers. """

    return [factorial(num) for num in nums]

numbers = [1, 2, 3, 4, 5]

for index, fact in enumerate(factorialize(numbers)):    
    print("Factorial of", numbers[index], "is", fact)

It prints:

Factorial of 1 is 1
Factorial of 2 is 2
Factorial of 3 is 6
Factorial of 4 is 24
Factorial of 5 is 120
input_set = [1, 2, 3, 4, 5]
fact = 1
for item in input_set:
    for number in range(1, item+1):
        fact = fact * number
    print "Factorial of", item, "is", fact
    fact = 1

Works as you need... Tested here[https://www.tutorialspoint.com/execute_python_online.php]

This should be your code. First, change your input_set to a list [] and not a dictionary.
Second, "input" is not a keyword that you have used, you have named it item.

You forgot to reset the factorial variable after an iteration.

input_set = {1, 2, 3, 4, 5}
for item in input_set:
    fact = 1
    for number in range(1,item+1):
    print fact
    print number
        fact = fact * number
    print ("Factorial of", item, "is", fact)

Its about your variable >>fact<< you placed in inside first for loop and your code is good.

what happening here is your fact=1 initialise value only once before entering loop while you need to do it each time out loop get execute.

input_set = [1, 2, 3, 4, 5]
for item in input_set:
    fact = 1
    for number in range(1,item+1):
        fact = fact * number
    print ("Factorial of", item, "is", fact)

hope this helps and do little R&D on variable scope :)

It's easier than it seems. You just need to place the fact/factorial variable inside the first loop. So that it gets reset every time the loop runs.

for number in [1, 2, 3, 4, 5]:
   factorial = 1
   for i in range(1, number + 1):
      factorial *= i
   print(f"Factorial of {number} is {factorial}")

Thanks, Ethan Lal

Related