返回函数外部错误需要帮助

2024-09-27 21:28:46 发布

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

def getPressAve(odbname):
    odb=openOdb(odbname)
    lastFrame=odb.steps['Step-1'].frames[-1]
    pressure=lastFrame.fieldOutputs['CPRESS']
    press=[[0,0]] # sets the first element to [0,0]

    for n in pressure.values:
      gridPt=part1.nodes.getFromLabel(n.nodeLabel)

      coord=assemb.getCoordinates(gridPt)
      press.append([n.nodeLabel,n.data,coord])
      press=avePress=press[1:] # removes the first element
      press.sort(Comp_X)
      print ('pressure extracted')

      index=0
      while index<len(press):
        sum=0
        tally=0

        if index!=0:
          sum=sum+press[index-1][1]
        tally=tally+1

        if index!=1:
          sum=sum+press[index-2][1]
        tally=tally+1

        if index!=2:
          sum=sum+press[index][1]
        tally=tally+1

        if index<len(press)-1:
          sum=sum+press[index+1][1]
        tally=tally+1

        if index<len(press)-2:
          sum=sum+press[index+2][1]
        tally=tally+1

        average=sum/tally
        avePress[index][1]=average
        index=index+1

    odb.close()
    print ('pressure averaged')
    return avePress

Tags: theindexlenifelementfirstpresssum
2条回答

您忘记正确缩进代码:

def getPressAve(odbname):
    odb=openOdb(odbname)
    ...
    print ('pressure averaged')
    return avePress

正如您的一样,return关键字在函数之外,因此出现了错误:SyntaxError: 'return' outside function。你知道吗

在Python中,缩进很重要。实际上,您正在定义一个名为getPressAve的函数,它只执行以下操作:

odb=openOdb(odbname)

定义完函数后,继续执行

lastFrame=odb.steps['Step-1'].frames[-1]

而这样的在函数之外。那不是你想要的。解决方案是将odb=openOdb(odbname)行之后的所有内容缩进到该级别,因此这些行被解释为函数体的一部分。你知道吗

相关问题 更多 >

    热门问题