math.prod() 在 google colab notebook 中不工作

math.prod() is not working in google colab notebook

我已经在 Gooogle ColabNotebook 中执行了这段代码...根据下面给出的 link... prod() 在 math 模块下可用。但是为什么 prod() 不工作..它给我错误? https://www.w3schools.com/python/ref_math_prod.asp

import math 
print(math.prod([1,2,3,4,5,6,7]))

输出 AttributeError: 模块 'math' 没有属性 'prod'

math.prod() 是 Python 3.8 及更高版本中可用的新函数。 Google Colab 的内核,截至撰写本文时,runs Python 3.6.9。因此,math.prod() 将无法供您使用。

您可以尝试 ,但似乎有些人的结果不一,接受的答案中的方法有点老套,但可能适合您的目的。

Google Colab 使用 Python 3.6。 9 截至 2021 年。math.prod() 在 python 3.8 版本中

你可以用这个

from functools import reduce
import operator

print(reduce(operator.mul, [1,2,3,4,5,6,7], 1))