Here is the task: Generate 20 random integers in the range from -5 to 4, write them to the cells of the array. Count how many positive, negative and zero values ​​are among them. Display array elements and counted quantities. Here is the text of the error:
a [i] = int (a [i]) TypeError: 'int' object is not subscriptable. How to fix this? Thank you very much!

import random a=[] for i in range(20): a=random.randint(-5,4) p=[] n=[] z=[] for i in range(20): a[i]=int(a[i]) for i in range(0,20,1): if(a[i]>0): p.append(a[i]) if(a[i]==0): z.append(a[i]) if (a[i]<0): n.append(a[i]) print('quantity positive',len(p)) print('quantity negative',len(n)) print('quantity zero',len(z)) 

    1 answer 1

    In the 4th line, you write an integer into the variable "a":

     a=random.randint(-5,4) 

    Then, in the 9th line you are trying to access the variable "a" by index.

     a[i]=int(a[i]) 

    But integer variables do not know how to index. To make it work, you need to replace the 4th line with this:

     a.append(random.randint(-5,4)) 

    In general, your code is not too pythonous. Where possible, it is recommended to use list inclusions instead of cycles. They are much more concise, and the code with them becomes much easier than without them.

    Your program can be rewritten in the following way:

     import random a = [random.randint(-5,4) for _ in range(20)] p = [i for i in a if i > 0] n = [i for i in a if i < 0] z = [i for i in a if i == 0] print('quantity positive', len(p)) print('quantity negative', len(n)) print('quantity zero', len(z)) 

    As you can see, it turned out much clearer than yours.

    PS: A sign of good style is considered to be spaces on the left and right of "equal", comparison signs and arithmetic symbols. And also a space after a comma.