用Python打印输出

用Python打印输出,python,Python,我正在尝试打印返回函数的结果。你能告诉我为什么我会收到错误信息吗: <function piecewise at 0x106a12840> 打印函数结果的正确方法是什么 谢谢 units={} g=9.81 units['g'] = 'm/s**2' # Mass density of water rho_w = 1031. units['rho_w'] = 'kg/m**3' # Mass density of oil rho_oil=859.870 units['rho_

我正在尝试打印返回函数的结果。你能告诉我为什么我会收到错误信息吗:

<function piecewise at 0x106a12840>
打印函数结果的正确方法是什么

谢谢

units={}

g=9.81
units['g'] = 'm/s**2'

# Mass density of water
rho_w = 1031.
units['rho_w'] = 'kg/m**3'

# Mass density of oil
rho_oil=859.870
units['rho_oil'] = 'kg/m**3'

# Heat capacity
cp_w =  4185.5
units['cp_w'] = 'J/(kg*K)'

# Expansion coefficient of water
alpha_w = 2.0e-4
units['alpha_w'] = '1/K'

# Dynamic viscosity of water
mu_w = 1.08e-3
units['mu_w'] = 'Pa*s'

# Kinematic viscocity of air
nu_a = 1.48e-5
units['nu_a'] = 'm**2/s'


def get_R(d, rho=rho_w, delta_rho=(rho_w-rho_oil), g=9.81, mu=mu_w):
    ''' Gets the R variable, used to calculate rise velocity '''
    import numpy as np
    Nd = 4*rho_w*delta_rho*g*(d**3)/(3*mu_w**2)
    conds=[ Nd<=73, (73<Nd)*(Nd<=580), (580<Nd)*(Nd<=1.55e7) ]
    funcs=[]
    funcs.append( lambda Nd: Nd/24 - 1.7569e-4*(Nd**2) + 6.9252e-7*(Nd**3) - 2.3027e-10*(Nd**4) )
    funcs.append( lambda Nd: np.power(10, -1.7095 + 1.33438*np.log10(Nd) - 0.11591*np.log10(Nd)**2) )
    funcs.append( lambda Nd: np.power(10, -1.81391 + 1.34671*np.log10(Nd) - 0.12427*np.log10(Nd)**2 + 0.006344*np.log10(Nd)**3) )
    return np.piecewise(Nd, conds, funcs)

print(np.piecewise)

当我运行你的代码时,我得到

NameError: name 'np' is not defined
你的进口

import numpy as np
位于函数get\R中。您应该将导入移到文件的顶部,以使np可用

如果要打印函数的输出,请执行以下操作

print(get_R(123))

在python中调用函数时,返回函数返回的内容。在此代码中,确保导入numpy并为变量d赋值:

result = get_R(d, rho=rho_w, delta_rho=(rho_w-rho_oil), g=9.81, mu=mu_w)
print(result)

你收到了什么错误?错误是什么?不清楚你想做什么。您定义了一个函数,但从不调用它。然后打印一个Numpy函数,但不要调用它。最后一个调用printnp.piecewise是不对的-我相信你的意思是printgetrdu,这不是一个错误。这是传递给“打印”的对象的字符串表示形式,恰好是“分段”函数“。如果您想要调用该函数的结果,则必须调用该函数。这可能只是解决方案的一部分,因为printnp.piecewise并不像@TrevorKeller的评论中所指出的那样真正有用。我尝试下一步运行“print get_R123”,它打印了0.0,没有错误,因此我认为这解决了问题