I'm pretty new to the python programming and I am currently trying to create a factorial function which I have, but now I an trying to get it to list the positive integer n as input and then computes and outputs, in separate lines, 1!, 2!, 3!, ..., (n-1)!, n! and so. I have come up with a code that outputs n! already.

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

any help will be greatly appreciated

Recommended Answers

All 2 Replies

Have you tried inserting a print statement into your function?

Easily done by not putting the recursive call into the return function, use temporary variable, something like this:

def factorialp(n):
    if n < 1:
         return 1
    q = n*factorialp(n-1)
    print "%d! = %d" % (n, q)
    return q

factorialp(5)

"""
result -->
1! = 1
2! = 2
3! = 6
4! = 24
5! = 120
"""
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.