繁体   English   中英

如何在定义的新函数中使用定义的函数?

[英]how can I use a function I define in a new function I define?

我定义的第一个功能有效

def chainPoints(aa,DIS,SEG,H):
#xtuple
    n=0
    xterms = []
    xterm = -DIS
    while n<=SEG:
        xterms.append(xterm)
        n+=1
        xterm = -DIS + n*SEGL
#
#ytuple
    k=0
    yterms = []
    while k<=SEG:
        yterm = H + aa*m.cosh(xterms[k]/aa) - aa*m.cosh(DIS/aa)
        yterms.append(yterm)
        k+=1

但是现在我需要一个依赖于我的第一个功能的第二个功能,特别是列表xterms和yterms。

def chainLength(aa,DIS,SEG,H):
    chainPoints(aa,DIS,SEG,H)

#length of chain
    ff=1
    Lterm=0.
    totallength=0.
    while ff<=SEG:
        Lterm = m.sqrt((xterms[ff]-xterms[ff-1])**2 + (yterms[ff]-yterms[ff-1])**2)
        totallength += Lterm
        ff+=1
return(totallength)

我没有定义函数就完成了所有工作,但是现在我需要为每个部分定义函数。

你需要从返回结果chainPoints()函数,然后在您指定的返回值本地名称(S) chainLength()函数:

def chainPoints(aa, DIS, SEG, H):
    #xtuple
    n = 0
    xterms = []
    xterm = -DIS
    while n <= SEG:
        xterms.append(xterm)
        n += 1
        xterm = -DIS + n * SEGL
    #
    #ytuple
    k = 0
    yterms = []
    while k <= SEG:
        yterm = H + aa * m.cosh(xterms[k] / aa) - aa * m.cosh(DIS / aa)
        yterms.append(yterm)
        k += 1

    return xterms, yterms


def chainLength(aa, DIS, SEG, H):
    xterms, yterms = chainPoints(aa, DIS, SEG, H)

    ff = 1
    Lterm = 0.
    totallength = 0.
    while ff <= SEG:
        Lterm = m.sqrt((xterms[ff] - xterms[ff-1]) ** 2 + 
                       (yterms[ff] - yterms[ff - 1]) ** 2)
        totallength += Lterm
        ff += 1

    return totallength

我在这里在chainLength使用了相同的名称,但这不是chainLength

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM