82:递归函数_阶乘计算案例

【操作】 使用递归函数计算阶乘(factorial)

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


for i in range(1, 6):
    print(i, '!=', factorial(i))

输出结果:

D:\work\python\two\venv\Scripts\python.exe D:\work\python\two\mypy18.py 
1 != 1
2 != 2
3 != 6
4 != 24
5 != 120

posted @ 2022-11-19 14:16  竹蜻蜓vYv  阅读(40)  评论(0编辑  收藏  举报