def factorial(n): if n == 0: return 1 else: return n * factorial(n-1)
时间: 2024-05-24 07:10:36 浏览: 191
下面是一个用Python编程求阶乘和数的例子: def factorial(n): if n == 0 or n ==
This is a recursive function to calculate the factorial of a given number.
The factorial of a number is the product of all integers from 1 to that number. For example, the factorial of 5 is 5 x 4 x 3 x 2 x 1 = 120.
The function takes in a parameter 'n', which is the number whose factorial is to be calculated. If the number is 0, the function returns 1 (since the factorial of 0 is 1). Otherwise, it recursively calls itself with n-1 as the parameter and multiplies the result with n. This continues until n reaches 0, at which point the function returns the final result.
阅读全文