擅长:python、mysql、java
<p>似乎您正在混合名称空间,因为您正在使用<code>from ... import *</code>。您想使用<code>sympy.exp()</code>,但您的代码使用<code>math.exp()</code>。保持名称空间的分隔是一个很好的实践,也就是说,不要使用<code>from ... import *</code>-一开始它看起来像是更多的输入,但最终会产生更清晰、更容易理解的代码。
尝试:</p>
<pre><code>import sympy as sy
import sympy.physics.units as units
def planck_law(wavelength,temperature):
"""Planck's Law evaluation at a single wavelength and temperature """
T=temperature
f=units.c/wavelength
h=units.planck
k=units.boltzmann
U=2*h/(units.c**3)*(f**3)/(sy.exp(h*f/(k*T))-1)
return U.evalf()
# Test:
print(planck_law(640e-9*units.m, 500*units.K))
# Result: 1.503553603007e-34*kg/(m*s)
</code></pre>