The code is not correct yet! You can find it here: https://repl.it/BTRP

In this WSQ I wanted to try all three approaches: a while-loop, a for-loop and recursion. In the beginning I had some trouble with the recursion but then I looked up how it is done in c++ and adapted the idea to Python 3 code.

Here it is:

def fac():
    
    n = int( input("Hey, I will calculate the value of n! (n factorial). Give me a non-negative integer value for n. "))
    
    def whileloop(num):
        x = 1
        while num > 1:
            x = x * num
            num = num - 1
        return(x)
    
    def forloop(num):
        y = 1
        for i in range(1,num+1):
            y = y * i
        return(y)
    
    def rec(num):
        if num > 0:
            z = num * rec(num-1)
            return(z)
        else:
            return(1)
    
    print("The approach with a while-loop gives n! = ", whileloop(n))
    
    print("The approach with a for-loop gives n! = ", forloop(n))
    
    print("The approach with recursion gives n! = ", rec(n))
    
    yesno = str( input("Do you want to do it again? Type y. Else type n."))

fac()

while yesno == "y":
    fac()
else:
    print("Okay, goodbye!")

CC BY 4.0 WSQ09 – Factorial Calculator by finntec is licensed under a Creative Commons Attribution 4.0 International License.