This program will print a range of factorials from 0 to the number given by the user.
def fact(x):
if x <= 1:
return 1
else:
return x * fact(x-1)
I begin by defining my function. Every recursive function has two major cases: a base case, and a recursive case. In this program, the base case is x being less then or equal to 1. The recursive case multiplies x by the result of the factorial function, called with the argument of 1 less than x.
for e in range(int(input("please input a number: "))):
print(e, fact(e))
Here, I call in the range function using the user’s input as an argument. Inside of it I print the variable that is being iterated upon, and its factorial.