Sami Sami - 2 months ago 7
Python Question

Use list of integer type in a loop in python

I have the following code:

a=[]
b=[]

for s in range(10):
dw = s%5
if dw == 1:
WD = random.randint(60,100)
DD =[int(round(dc*WD,0)) for dc in [.2,.2,.2,.2,.2]]
for k in range(5):
a.append(DD[k])
print a
TCV = DD[dw]
DDPT = [int(round(pt*TCV)) for pt in [.3,.5,.2]]
for i in range(3):
b.append(DDPT[i])
for PT in range(3):
for p in DDPT[PT]:
print 't'


After running the code I get this error:

for p in DDPT[PT]:

TypeError: 'int' object is not iterable.


I was wondering if someone can help me in this regard.

Thanks!

Answer

DDPT is an array of integers, as evidenced in this line:

DDPT = [int(round(pt*TCV)) for pt in [.3,.5,.2]]

DDPT[PT] is some integer, and you are trying to iterate through that. Hence the error.

I would recommend giving your variables more descriptive names so these issues are easier to debug.

edit:

for num_iters in DDPT: for iteration in range(num_iters): do_something()

Comments