Control Flow: The for
Loops
Factorial Function
An alternative form of range()
function can be used to specify the numbers to be included in the return list as in:
def calculate_factorial(number):
factorial = 1
for i in range(1, number+1):
factorial = factorial * i
return factorial
x = int(input("Enter a number for x: "))
y = int(input("Enter a number for y: "))
xFactorial = calculate_factorial(x)
yFactorial = calculate_factorial(y)
print(x, "! =", xFactorial)
print(y, "! =", yFactorial)
print("x! * y! =", xFactorial * yFactorial)
print("(x+y)! =", calculate_factorial(x+y))
where the values in the range [1,number]
(number
included) will be assigned to the variable i
in each iteration of the for i in range(1, number+1):
loop.