HelloWorld
HelloWorld

Reputation: 93

Python, For loop counter do not update?

I am writing a simple program which gives the list of all prime no. less than a given no. N. I am facing a problem that my for loop counter check in the function primes_gen else clause do not updates. Though, when i use the same logic without a function, as mention in the second code snippet, everything works correct.

Logic applied:-

  1. when n passed to function is <= 2, blank list is returned

  2. when n is > 2, range(3,n,2) identifies all the odd number. This is step 1 marked in first code snippet.

  3. second for loop, marked with step 2, checks if no. is divisible to the all no.'s less than i. If yes then check counter is incremented by +1 and this loop is ended with 'continue'

  4. if check remains zero till the end of the for loop then i is appended to the list and the finally after completion of the loop list is returned.

Problem:-

-The list i got as a result was [2] which is wrong ans. so I wrote step 3, as marked in 1st snippet, and i found that my check counter do not updates after the 'continue', as shown in the output. when same logic is applied without a function as written in second code snippet everything works correct. I am not able to get what is going wrong here, why does check do not updates after continue?

contents =['10']

def prime_gen(n):
    num_list=[2]
    if n <=2:
        return []
    else:
        for i in range(3,n,2):    #step 1
            check=0
            for u in (2,i):       #step 2
                if i%u == 0:
                    check += 1
                    continue
            print (check,i)     #step 3
            if check == 0:
                num_list.append(i)
    return num_list

if __name__== '__main__':
    for j in range(len(contents)):
            print (int(contents[j]))
            num_list = prime_gen(int(contents[j]))
            print (str(num_list).replace('[','').replace(']',''))

Output

 10
 1 3
 1 5
 1 7
 1 9
 2

Expected ans is (when step 3 above is commented)

10
2, 3, 5, 7

Second code without function

contents = ['10']   #to-check


for i in range(len(contents)):
    target = int(contents[i])
    num_list= [2]
    for j in range(3,target,2):
        check = 0
        for u in range(2,j):
        if j%u == 0:
            check +=1
            continue
    if check == 0:
        num_list.append(j)
#print (num_list)
print (str(num_list).replace('[','').replace(']',''))

output

2, 3, 5, 7

Upvotes: 0

Views: 1285

Answers (1)

Blair
Blair

Reputation: 6693

Your problem took a second to find, but is very simple to solve. The issue is at your "Step 2". You have the line

for u in (2,i):       #step 2

That iterates u through all the variables in the tuple you provide, namely 2 and i. Of course, i%i will be 0, so check will always be true. What you want instead is this:

for u in range(2,i):       #step 2

This will iterate u through all the variables between 2 and i, as you intend.

See these two examples:

>>> i = 7
>>> for u in (2,i): print u,

2 7
>>> for u in range(2,i): print u,

2 3 4 5 6

Basically, you just forgot to use range in your for loop, and that's going to give you wildly different results than what you actually want.

Upvotes: 4

Related Questions