问题描述:
解题思路:
定义一个阶乘函数和存放结果的变量result,如果输入数字num,num本身与(num-1)相乘的结果存放到result变量中,每次相乘num减一,直到num=1为止,result即为num的阶乘
代码:
def number_factorial(num):
if isinstance(num, int):
result = num
while num > 1:
result = result * (num -1)
num = num -1
return result
print("3的阶乘为:",number_factorial(3))
print("4的阶乘为:",number_factorial(4))
print("6的阶乘为:",number_factorial(6))
效果: