如何修复返回“none”的函数导致pow()的操作数不受支持的问题

2024-09-27 23:20:58 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图在我的另一个函数“TotalUnderstance”中调用我的函数“StatisticalUnderstance”。但是,当我执行StatisticalUnderstance的输出时,它返回一个数字和“none”,这给了我一个错误。为什么当我在完全不确定的情况下使用它时,它会返回“无”,而当我单独使用它时,它不会返回“无”

我尝试过不定义typeAUnc,而只是将statisticalUncertability(扩展数据)直接放入我对totalUnc的定义中,但这仍然会得到相同的错误

import numpy as np

xdata1=[72.2,77.6,82.4,86.3,88.9]
xdata2=[80.10,81.45,81.50,81.34,82.01]


def statisticalUncertainty(xdata):
   n = len(xdata)
   meanXdata=np.mean(xdata)
   for i in range(1,n):
      innerSum=0
      innerSum=innerSum+(xdata[i]-meanXdata)**2

   std = np.sqrt(innerSum*(1/(n*(n-1))))

   print(std)


def totalUncertainty(xdata,typeBUnc):
   typeAUnc = statisticalUncertainty(xdata)
   totalUnc = 2*(np.sqrt((typeAUnc)**2)+((typeBUnc)**2))

   print(totalUnc)
totalUncertainty(xdata1,0.5)

我得到的错误是: TypeError: unsupported operand type(s) for ** or pow(): 'NoneType' and 'int'


Tags: 函数for定义def错误npstdxdata
2条回答

您的函数statistical只打印值,不返回值。 您应该在该函数中使用return而不是print

您需要向StatisticalUncertability函数添加一个返回值。这段代码很好用,我假设你想要什么

import numpy as np

xdata1=[72.2,77.6,82.4,86.3,88.9]
xdata2=[80.10,81.45,81.50,81.34,82.01]


def statisticalUncertainty(xdata):
   n = len(xdata)
   meanXdata=np.mean(xdata)
   for i in range(1,n):
      innerSum=0
      innerSum=innerSum+(xdata[i]-meanXdata)**2

   std = np.sqrt(innerSum*(1/(n*(n-1))))

   print(std)
   return std


def totalUncertainty(xdata,typeBUnc):
   typeAUnc = statisticalUncertainty(xdata)
   totalUnc = 2*(np.sqrt((typeAUnc)**2)+((typeBUnc)**2))

   print(totalUnc)
totalUncertainty(xdata1,0.5)

相关问题 更多 >

    热门问题