【问题标题】:math module in python (syntax)python中的数学模块(语法)
【发布时间】:2022-07-23 23:13:37
【问题描述】:
import math
for _ in range (int(input())):
     n = int(input())
     A = list(map(int, input().split()))
     final=[]
     for  i in range(len(A)-1):
         if i==len(A):
             final=final.append(A)
         else:
             for p in range (i+1,len(A)-1):
                 new_list = A[i : p]
                 product= math.prod(new_list)
                 final = final.append(product)
     print(sum(final))

错误

Traceback (most recent call last):
  File "D:\python\codechef\problem.py", line 12, in <module>
    product= math.prod(new_list)
AttributeError: module 'math' has no attribute 'prod'

为什么会这样显示?

【问题讨论】:

  • 您使用的是哪个版本的 Python?在 v3.8 中添加了math.prod

标签: python


【解决方案1】:

math.prod 是一个新函数(来自 Python 3.8)。如果您想使用旧版本的 Python 运行,请像这样使用

from functools import reduce
import operator

reduce(operator.mul, [1, 2, 3, 4], 1)

【讨论】:

    猜你喜欢
    • 2012-02-05
    • 2019-03-22
    • 2018-03-17
    • 1970-01-01
    • 2011-01-05
    • 1970-01-01
    • 2014-04-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多