1
votes

I'm slowly learning Python on my own so here is a problem I've been running into.

Trying to step through each number up to the last number and depending on the range it steps into then a calculation will be made.

NUM = []
for b in range(1,8760):
  if 3000 < b < 7000:
    NUM=500
  else:
    NUM=300
writer.writerow([NUM])

TypeError: 'int' object not iterable

I also tried this below and it runs but only prints out one number for the whole list instead of choosing between the two number options...

NUM = []
for b in range(1,8760):
  NUM = numpy.where((b > 3000) & (b < 7000), 500, 300)
writer.writerow([NUM])
2
Can you paste the exact error and the line it refers to? - Idos
NUM=300 - Do you understand that NUM is no longer a list at this point? - Fred Larson
You are welcome! If it helped, please upvote my answer and mark it as correct :) - Dennis C Furlaneto

2 Answers

1
votes

Did you mean this instead?

NUM = []
for b in range(1,8760):
  if 3000 < b < 7000:
    NUM.append(500)
  else:
    NUM.append(300)
writer.writerow(NUM)
1
votes

You're changing NUM inside your loop to a number either 500 or 300.

I think what you're looking for is to append to your list

Try:

NUM.append(500)

or

NUM.append(500)

This will add to NUM and you will end up with a list of 500s and 300s depending on your if condition.

You can see how this structure works here in the python documentation.