0

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?

5
  • What is 2((-2**a)/(2**a)+2) supposed to do then? Are you forgetting a * multiplication there? Commented Apr 11, 2015 at 11:47
  • 1
    In your second example you simply removed the b = 0 line; that's indeed going to lead to a new error. Commented Apr 11, 2015 at 11:48
  • The ** is for a formula. Commented Apr 11, 2015 at 11:55
  • ** is the exponentiation operator and not what I am talking about. I am talking about the 2 at the start. I think you meant to multiply by 2 here. Commented Apr 11, 2015 at 11:56
  • Yeah, your answer is correct. I'll mark it when I can. Proves how much of a rookie I am >.< Commented Apr 11, 2015 at 11:59

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.