0

I have one array 'barray' of size 'bsize' and another 'carray' of size 'csize'. The i loop is for barray and j loop is for carray.

I get an error that i is not defined. I want the loops to go from 0 to bsize - 2 in steps of 3, and 0 to csize - 2 in single steps.

How should I relate the size and array to the for loop?

bsize = 960
csize = 960
barray = bytearray(fi.read())
carray= bytearray(f1.read())



for i in range (bsize-2,i+3):
    for j in range (csize-2,j+1):
2
  • So, what exactly do you want i to be in the first loop? 0 to bsize - 2? bsize - 2 to something? Commented Aug 15, 2013 at 23:33
  • I want i to take the barray from 0 to bsize-2 and increment by 3 and j to take carray ffrom 0 to csize-2 and increment by 1 Commented Aug 15, 2013 at 23:36

1 Answer 1

4
 for i in range (0, bsize - 2, 3): #possibly bsize - 1?
    for j in range (csize - 2): # possibly csize - 1?
        #do your thing

That will loop through the first one incrementing i by 3 every time, and j by 1.

Look at this tutorial or these docs to learn range, it's really useful!

I'm not sure if you want to go through bsize - 2 or just up to it. If through, use size - 1 to get size - 2.

The reason you're getting an error is that you haven't defined the i you're using in the step. As you can see, python's range isn't like a lot of other languages' for constructs. Once you get used to it, though, it's really flexible and easy to use.

Some examples using simple range:

>>> for i in range(0, 14, 3):
...    print i
... 
0
3
6
9
12

>>> for i in range(1, 5):
...     print i
... 
1
2
3
4

>>> for i in range(5):
...     print i
... 
0
1
2
3
4
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.