0
def square(x):
    int(x)
    return 2(x * x)

def my_map(func, arg_list):
    result = []
    for i in arg_list:
        result.append(func(i))
    return result

squares = my_map(square, [1,2,3,4,5])

print(squares)

i'm trying to pass a number to a function and have it print the result, i have a function for the equation def square(), and a function that receives the numbers and the function

I keep on getting this error:

"TypeError: 'int' object is not callable"

I'm new to programming and was watching corey shaffer in YouTube and he wrote a program similar to this one. I started to play around with it, and now I'm stuck.

I would like the print statement to print out arg_list(i) and have I go through def square(x) and have that answer stored in result

abdullahalali
  • 396
  • 2
  • 5
  • 18
  • 1
    What are you trying to do with `2(x * x)` ? – Devesh Kumar Singh Nov 22 '19 at 18:04
  • so in the list that i have passed in squares , i want it to go through each number individualy. so exp: def square(2): return 2(2*2) witch will give me an answer of 8 – anthony pizarro Nov 22 '19 at 18:05
  • Concatenation isn't multiplication in Python. Writing `2` and `(x * x)` next to each other doesn't mean multiply them. – user2357112 Nov 22 '19 at 18:12
  • Also note that `int(x)` doesn't do anything. It converts the value of x to an integer, but doesn't store it anywhere. Maybe you'ld want `x = int(x)` ? – JohanC Nov 22 '19 at 19:33
  • error means that somewhere in code you assigned value to varaible `int` - ie. `int = 2` - so now it can't access function `int()` - check `print(int)` and `print(type(int))` – furas Nov 22 '19 at 20:09

3 Answers3

3

here's a fix to your square function (you were missing a * operator)

def square(x):
    int(x)
    return 2*(x * x)

however, according to your function name I'm guessing you wanted the function to return the square of x:

2 --> 4
3 --> 9
4 --> 16

in that case, here's also a bug fix:

def square(x):
   return x**2
Aviad Rozenhek
  • 2,259
  • 3
  • 21
  • 42
2

The line 2(x * x) is causing python to treat the integer 2 as a function with arguments x*x, and hence the error "TypeError: 'int' object is not callable"

In [16]: x = 1                                                                                                 

In [17]: 2(x*x)                                                                                                
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-17-14e610e542ba> in <module>
----> 1 2(x*x)

TypeError: 'int' object is not callable

To square a number, you need x**2 instead, or perhaps pow(x, 2)using the pow builtin

def square(x):
    int(x)
    return x**2
Devesh Kumar Singh
  • 20,259
  • 5
  • 21
  • 40
  • thanks but i dont understand how python is taking 2 as a function. i feel like this is a python subject that i can probably read somewhere. but how should i look for it in google. – anthony pizarro Nov 22 '19 at 18:10
  • 2 is actually an object of type int. `In [23]: type(2) Out[23]: int` as in python, integers are treated as an object and you also see that in the error – Devesh Kumar Singh Nov 22 '19 at 18:11
0

The problem is with 2(x * x) statement. In the Python programming language, we need to explicitly mention all the operators in expressions. Like, the statement should be 2*(x*x) and when you use rounded brackets, you are actually calling the function.

Also, the above program can be reduced to,

In [54]: list(map(lambda no: int(no)**2, [1, 2, 3, 4, 5]))
Out[54]: [1, 4, 9, 16, 25]
Premkumar chalmeti
  • 800
  • 1
  • 8
  • 23