Home > OS >  Input a list of numbers and and replace each element in the list with its factorial(use factorial()
Input a list of numbers and and replace each element in the list with its factorial(use factorial()

Time:01-12

Hey I'm new to lists and modules can you show me how to do this. I actually tried this code but it seems to be giving me a wrong answer

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

fact = 1 for i in l: for number in range(1,i 1): fact=fact*number print ("Factorial of", i, "is", fact)

CodePudding user response:

def factorial(n):
    if n < 2:
        return 1
    else:
        return n * factorial(n-1)

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

for number in l:
    fact = factorial(number)
    print("Facotrial of ",number," is",fact)

Here is a working example.

You can use the balise code to better show your code, I have trouble to read it so I can't really comment on what you did wrong

  •  Tags:  
  • Related