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
2 回答
            1        
        
		
在 Python 中,缩进很重要。照原样,您正在定义一个名为的函数getPressAve,该函数仅执行此操作:
odb=openOdb(odbname)
在你定义了你的功能之后,你继续做
lastFrame=odb.steps['Step-1'].frames[-1]
和这样的功能之外。那不是你想要的。解决方案是将该odb=openOdb(odbname)行之后的所有内容缩进到该级别,因此这些行被解释为函数体的一部分。
于 2013-06-05T05:08:59.563   回答
    
    
            0        
        
		
您忘记正确缩进您的代码:
def getPressAve(odbname):
    odb=openOdb(odbname)
    ...
    print ('pressure averaged')
    return avePress
正如你所没有的那样,return关键字出现在函数之外,因此错误:SyntaxError: 'return' outside function。
于 2013-06-05T05:08:48.910   回答