【发布时间】:2020-04-17 01:55:21
【问题描述】:
我试图将列表中的所有数字相乘
a = [2,3,4]
for number in a:
total = 1
total *= number
return total
它的输出应该是 24,但由于某种原因我得到了 4。为什么会这样?
【问题讨论】:
-
"total" 在每次循环迭代中设置为 1。你不想这样。
-
从 Py3.8 开始:
import math、product = math.prod([2, 3, 4])。
标签: python list math operator-keyword multiplication