python - 继续获取,类型错误 : a float is required

标签 python typeerror

我不断收到类型错误,但我无法找出导致问题的原因。 我的代码是..

#import modules needed
from math import sin, pi, sqrt
from cmath import exp
from lab2_q4b import q

#Define constants
lamda=500e-6 #wavelength of the incident light, units in meter
n=10 #number of slits in a dffraction grating
f=1.0 #focal length, units in meter
x=0.1 #width of the screen, units in meter

#Define a new function called qnew, which will be our integrand
def qnew(u):
    return sqrt(q)*exp(1j*2*pi*x*u/(lamda*f))

我运行这个它给了我错误..

这是 q.. 的代码

#Import modules
from math import sin, pi


#Define a function q that takes u as an argument

def q(u): #transmission function 
    d=20e-6 #separation of slits, units in meter
    alpha=pi/d #

    return (sin(alpha*u))**2 #the result of the transmission function 

最佳答案

问题出在qnew的这个特定部分:

sqrt(q)

在隔离中运行这段代码将引发您所看到的异常:

>>> sqrt(q)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: a float is required

q 是一个函数,因此您尝试取函数的平方根,这当然没有意义。我不确定您打算在其位置放置什么,但确定您需要修复的区域。

关于python - 继续获取,类型错误 : a float is required,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25880268/

相关文章:

python - Crontab 停止 - Ubuntu 服务器的管道损坏

python - 类型错误 ('module' 对象不可调用)使用 dateutil relativedelta

Python 2.7/App Engine - TypeError : is_valid() takes exactly 2 arguments (3 given)

python - 为什么代码显示 'int' 对象不可下标?

python - 根据标准分析一列数据框的数据

python - 在python中的while循环中更新 "To:"电子邮件 header

python - 查询时间过长时 psycopg2 游标挂起

python - 在 Mac 11.0.1 上运行 Django 站点时出现 AttributeError

python - 类型错误:缺少一个必需的位置参数 'event'

javascript - 用Java编写Conways Life of Life错误