小编Joe*_*Ngo的帖子

在python中使用for循环的factorial

result = 1
num = 1
while num <= 5: 
    result *= num 
    num = num + 1
print(result) #this gives me 5! 
Run Code Online (Sandbox Code Playgroud)

但是,我想用for循环做这个.我试过用这个

result = 1
for num in (1, 6, 1): 
    result *= num 
    print(result) #just to see intermediate calculations
print(result)
Run Code Online (Sandbox Code Playgroud)

结果我得到= 6而不是120.

1
6
6 
Run Code Online (Sandbox Code Playgroud)

是我得到的输出.

我为for循环做了什么错误?

python for-loop

0
推荐指数
1
解决办法
3438
查看次数

标签 统计

for-loop ×1

python ×1