python错误:参数必须是字符串或数字,而不是函数

时间:2020-06-01 08:45:55

标签: python python-3.x

我有以下代码:

import numpy as np
import math
x = np.arange(0, 1.1, h) #point in space
t = np.arange(0, 1.1, k) #point in time
#nodes in matrix form
m = len(x)
n = len(t)
T = np.zeros((m,n))

def ft0 (x):
f = lambda x: math.sin(math.pi*x)# Initial Condition at t=0
return f

for i in range (m-1):
   T[0][i] = ft0[i]

运行代码时,我得到:

TypeError:float()参数必须是字符串或数字,而不是 “功能”

我一直在网上搜索解决方案,但我不太了解该错误。

1 个答案:

答案 0 :(得分:0)

代码中存在多个问题。

    您提供的代码中未定义
  1. hk,我认为它们均为0.1
  2. 正如我在评论中提到的那样,您应该以{{1​​}}而不是您的方式调用该函数。
  3. 您返回定义的ft0(i)函数而不是结果,因此应将其更改为lambda而不是...return f(x),这会导致上述错误。

您可以在以下代码中看到以上内容:

...return f

注意

  1. 您仅分配给第一个import numpy as np import math def ft0(x): f = lambda x: math.sin(math.pi*x)# Initial Condition at t=0 return f(x) h=0.1 k=0.1 x = np.arange(0, 1.1, h) #point in space t = np.arange(0, 1.1, k) #point in time #nodes in matrix form #nodes in matrix form m = len(x) n = len(t) T = np.zeros((m,n)) for i in range(m-1): T[i][0] = ft0(i) print(T) ,您可能应该使用double for循环
  2. column像您所做的那样的函数中是不必要的情况下,您可以仅将lambda用作函数。我建议您在以下链接https://realpython.com/python-lambda/
  3. 中进行阅读
相关问题