小编And*_*ons的帖子

用于查找阶乘的 Python 代码问题

让函数FirstFactorial(num)接受传递的 num 参数并返回它的阶乘。例如:如果 num = 4,那么你的程序应该返回 (4 * 3 * 2 * 1) = 24。对于测试用例,范围将在 1 到 18 之间,输入将始终是一个整数。

这是我的代码

def FirstFactorial(num):
    x = [1]
    if num == 1:
        return 1
    else:
        for i in range(1,num+1):
            x = x*(i)
    return x

print (FirstFactorial(4))
Run Code Online (Sandbox Code Playgroud)

预期输出为24。我从上面给出的代码中得到以下输出。

[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
Run Code Online (Sandbox Code Playgroud)

python python-3.x

2
推荐指数
1
解决办法
774
查看次数

标签 统计

python ×1

python-3.x ×1