0

I am having trouble implementing a counter to count over iterations on a double for loop.

My code is the following:

def encode(mat,l,c,mode,m):
    sampleCount = 0
    for i in range(l):
        for j in range(c):
            sampleCount += 1

    print(sampleCount)

My program calls this function with values of "l" of 720 and 360 and values of "c" of 1280 and 640 respectively. What I was expecting was sampleCount values of 921600 and 230400. However, it prints either 1279 or 639. Also, when i tested printing i and j like this:

for i in range(l):
    print(i)
    for j in range(c):
        print(j)

What I get is the program printing all the i values, from 0 to l-1 and only then printing the j values from 0 to c-1.

Can anyone tell me what I may be doing wrong? Thanks in advance!

Edit: Pasted code without identation

Edit 2: Tried commenting everything after sampleCount += 1. In that case, i obtain the expected results. And it continues to work well if i uncomment the following two lines of code. However, when i tried uncommenting more than 3 lines of code, it goes back to misbehaving. In short, it works when the code is like this:

def encode2(mat,l,c,mode,m):
    sampleCount = 0
    for i in range(l):
        for j in range(c):
            sampleCount += 1
            a = 0
            b = 0
            # c = 0
            # x = 0
            # if (i == 0 & j == 0):
            #     a = 0
            #     b = 0
            #     c = 0
         ...  ...

And misbehaves again if the code is like this:

def encode2(mat,l,c,mode,m):
    sampleCount = 0
    for i in range(l):
        for j in range(c):
            sampleCount += 1
            a = 0
            b = 0
            c = 0
            # x = 0
            # if (i == 0 & j == 0):
            #     a = 0
            #     b = 0
            #     c = 0
         ...   ...
5
  • Well, i print them before calling the function and I know they are values of 1280 or 640 and 720 or 360 for l and c respectively. This code is part of a video encoder and my objective is to go through each of the "pixels" of the frame Commented Jan 6, 2020 at 14:09
  • Sounds like an indentation problem from the symptoms, you describe the second loop happens after the first completed, so I guess the interpreter doesn't see the relation between them as you expect Commented Jan 6, 2020 at 14:14
  • your encode function is empty. You are probably getting an error for having an empty function. Try solving this first Commented Jan 6, 2020 at 14:22
  • Sorry, I pasted the code wrong. In the program, the identation is correct. I will edit it Commented Jan 6, 2020 at 14:28
  • You are doing range(c) and inside the loop you do c=0. You cannot do that, the loop is using the variable c. Commented Jan 6, 2020 at 15:47

1 Answer 1

1

I got the following result when I ran the same code:

def encode(l,c):
    sampleCount = 0
    for i in range(l):
        for j in range(c):
            sampleCount += 1

    print(sampleCount)
encode(360,640)

Result: 230400

def encode(l,c):
    sampleCount = 0
    for i in range(l):
        for j in range(c):
            sampleCount += 1

    print(sampleCount)
encode(720,1280)

Result: 921600

The same is your expectation?

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

4 Comments

Yes, that is what i hoping to get. One thing I didn't mention is that there is more code after the sampleCount += 1. A bunch of variable assignments and if conditions, but with no breaks nor continues
Then most probably you are redefining some variable there, maybe you are reusing i or j for other purposes.
I saw your code. You are changing the value of c in c=0 which is being used for loop iteration i.e. range(c). So the loop count changes. And you are getting the wrong answer.
@VaibhavJadhav YES! That's exactly it! I feel so stupid right now. Thanks so much!!

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.