0

I want a loop to create an array of 11 integers y[i] such that y[i] = (i+1)*(i+2) and it gives me an error which I don't understand.

In [100]: y = zeros(11)
     ...: for i in range(11):
     ...:     y[i] = (x[i]+1)*(x[i]+2)
     ...:     
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-100-7a762b788eff> in <module>()
      1 y = zeros(11)
      2 for i in range(11):
----> 3     y[i] = (x[i]+1)*(x[i]+2)
      4 

TypeError: 'int' object has no attribute '__getitem__' 
2
  • 3
    Wait, your first sentence says y[i] = (i+1)*(i+2) but your code says y[i] = (x[i]+1)*(x[i]+2). One of those two must be wrong. Commented Jan 25, 2014 at 19:19
  • Where is x defined? You need to show use more. Commented Jan 25, 2014 at 20:25

2 Answers 2

4

x is an integer, not an array:

>>> x = i = 0
>>> x[i]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'int' object has no attribute '__getitem__' 

You need to scan back in your code and see what rebound x to an integer.

Your question however implies you think you are executing:

y[i] = (i+1)*(i+2)

but your actual code sample clearly shows you are not. Figure out which code should actually run here first.

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

Comments

1

Basically that means, that you try to do [] on an int so y or x in your code is an integer

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.