0

I've been wrestling with this 'int' object not callable error for a while and it's still showing up. Here's my code:

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)

and here's the error its producing:

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

I understand most of the time this error means I would have forgotten a '*' and the system reads it as a function 2, for instance, 2(1-2*n) instead of being multiplied. I don't see a mistake of that kind in my code so can someone help me find out what's going on. Thanks :)

1
  • Don't use builtin function names as variable names or you are asking for trouble Commented Mar 28, 2016 at 23:21

1 Answer 1

1

you commented it out but

range = int(2*x)

makes range an integer instead of a function ....

try to print stuff ... that will usually help you solve your problems

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
3
  • 1
    Might want to change sum too or that might be another question ;) Commented Mar 28, 2016 at 23:22
  • i see what you're saying, but range was only there because i was having a problem using numpy's linspace function. That's why it's commented out. But for future reference i didn't know that, so i'll be more careful on my variable naming ;P Commented Mar 28, 2016 at 23:22
  • this is the problem ... try adding a print where a told you to .... somewhere else you are makeing range an integer instead of a method Commented Mar 28, 2016 at 23:25

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.