Reputation: 865
I'm attempting to program a formula that loops until it reaches the number it is asked to loop for and give an answer to it. However, I seem to be getting variable/calling errors.
def infinitesequence(n):
a = 0
b = 0
for values in range(0,n+1):
b = b + 2((-2**a)/(2**a)+2)
a += 1
return b
returns
TypeError: 'int' object is not callable
while
def infinitesequence(n):
a = 0
for values in range(0,n+1):
b = b + 2((-2**a)/(2**a)+2)
a += 1
return b
returns
UnboundLocalError: local variable 'b' referenced before assignment
What is causing this error?
Upvotes: 0
Views: 37
Reputation: 1122232
2((-2**a)/(2**a)+2)
is trying to use that first 2
as a function. You are asking Python to call 2()
by passing in the result of the (-2**a)/(2**a)+2
expression, and that doesn't work:
>>> 2()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'int' object is not callable
Perhaps you were forgetting to use a *
multiplication operator there:
2 * ((-2 ** a) / (2 ** a) + 2)
Your UnboundLocal
error stems from you removing the b = 0
line, which was not the cause of your original error.
Upvotes: 2