'Int'对象在我的代码中不可调用错误

时间:2016-03-28 23:15:10

标签: python python-3.5

我一直在与这个'int'对象进行摔跤,暂时不会出现可调用错误,而且它仍在显示。这是我的代码:

import numpy as np
import matplotlib.pyplot as plt

def main_1(n):
    """executes part (a) of problem 1"""
    def c_n1(n):
        """Finds constants for n terms"""
        sum = 0
        for i in range(-n, n+1):
            sum += (((-1)**i)*27*np.sqrt(3)*i) / (2*np.pi*(1-9*(i**2)))
        return sum

    def f_n1(n, func):
        """function values using constant c_n values"""
        lmbda = 2
        k_n = (2*np.pi*n) / lmbda
        sum = 0
        x = 1
        #range = int(2*x)
        t_steps = 100
        #delta_t = range / t_steps
        f_t_values = np.zeros((t_steps))
        t = np.linspace(-x, x, t_steps)

        for i in t:
            for j in range(-n, n+1):
                sum += np.sin(k_n*i)*func(n)
            f_t_values[i] = sum
        return f_t_values, t

    def plot(f_t_values, t):
        """plots f_t function over interval t"""
        plt.plot(f_t_values, t)
        plt.show

    f_t_values, t = f_n1(n, c_n1)
    plot(f_t_values, t)
    print(f_t_values.shape)
    print(t.shape)

main_1(5)

q = np.arange(1,2,100)
print(q.shape)

这是产生的错误:

TypeError                                 Traceback (most recent call last)
<ipython-input-6-03432c50e4a9> in <module>()
     39     print(t.shape)
     40 
---> 41 main_1(5)
     42 
     43 q = np.arange(1,2,100)

<ipython-input-6-03432c50e4a9> in main_1(n)
     34         plt.show
     35 
---> 36     f_t_values, t = f_n1(n, c_n1)
     37     plot(f_t_values, t)
     38     print(f_t_values.shape)

<ipython-input-6-03432c50e4a9> in f_n1(n, func)
     24 
     25         for i in t:
---> 26             for j in range(-n, n+1):
     27                 sum += np.sin(k_n*i)*func(n)
     28             f_t_values[i] = sum

TypeError: 'int' object is not callable

我理解大多数时候这个错误意味着我会忘记'*'并且系统将其作为函数2读取,例如2(1-2 * n)而不是相乘。我在代码中没有看到这种错误,所以有人可以帮我找出发生了什么。谢谢:))

1 个答案:

答案 0 :(得分:1)

你评论了但是

range = int(2*x)

使范围成为整数而不是函数....

尝试打印东西...... 通常可以帮助您解决问题

def f_n1(n, func):
    """function values using constant c_n values"""
    lmbda = 2
    k_n = (2*np.pi*n) / lmbda
    sum = 0 # this might lead to a problem later....
    x = 1
    #range = int(2*x)
    t_steps = 100
    #delta_t = range / t_steps
    f_t_values = np.zeros((t_steps))
    t = np.linspace(-x, x, t_steps)

    for i in t:
        print(range) ### # now you will see that range is a number not a function at this point ... because you assigned a number to it
        for j in range(-n, n+1):
            sum += np.sin(k_n*i)*func(n)
        f_t_values[i] = sum
    return f_t_values, t