用Python打印输出

时间:2019-05-24 20:00:47

标签: 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_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)

2 个答案:

答案 0 :(得分:0)

运行代码时,我得到

NameError: name 'np' is not defined

您的导入

import numpy as np

位于函数 get_R 中。您应该将导入移动到文件顶部,以使np可用。

如果要打印函数的输出,请执行

print(get_R(123))

答案 1 :(得分:0)

当您在python中调用函数时,您将获得函数返回的内容。在此代码中,请确保导入numpy并将值分配给变量d:

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