Defining a function that will print each factorial of y from 0 up to a specified value

53 Views Asked by At

I'm trying to get the function input to be similar to

RangeFactorial(10)

and the function produce the factorials of each number from 0 to 10

Currently I have:

def RangeFactorial(n)
    result = 1
    for x in range(1, n +1):
         result = result * x
    return result

for n in range(1, 10):
    print(n, RangeFactorial(n))

The outcome is fine, but I would the range input be like I stated above.

Additionally, I want the output to be a list and I've no idea where to start with that.

1

There are 1 best solutions below

1
chepner On

The function you call RangeFactorial is just the regular factorial function:

def factorial(n)
    result = 1
    for x in range(1, n +1):
         result = result * x
    return result

RangeFactorial would be the function that calls factorial 10 times:

def range_factorial():
    for n in range(1, 10):
        print(n, factorial(n))

To build a list instead of printing the values, you can do, for example,

def range_factorial():
    result = []
    for n in range(1, 10):
        result.append(factorial(n))
    return result