while loop condition "suspicious behaviour"
code works fine except I can't figure out the while loop condition works unexpectedly as I know when we compare two values (x<y) loop should work as long as condition valid less and not equal but i have something else in the code below def check_prime(prime_list, new_number): if all(new_number%i!=0 for i in prime_list): prime_list.append(new_number) return new_number else: return 0 prime_numbers=[2,3] counter_prime=len(prime_numbers) new_number=3 while counter_prime <= 100: counter_prime=len(prime_numbers) new_number+=1 x=check_prime(prime_numbers,new_number) if (x !=0) and (x not in prime_numbers): prime_numbers.append(x) print(prime_numbers) [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547] if you check the length of prime_numbers = 101 same as to print the results with index for i,j in enumerate(prime_numbers): print(i,j) 0 2 1 3 2 5 3 7 4 11 5 13 6 17 7 19 8 23 9 29 10 31 11 37 12 41 13 43 14 47 15 53 16 59 17 61 18 67 19 71 20 73 21 79 22 83 23 89 24 97 25 101 26 103 27 107 28 109 29 113 30 127 31 131 32 137 33 139 34 149 35 151 36 157 37 163 38 167 39 173 40 179 41 181 42 191 43 193 44 197 45 199 46 211 47 223 48 227 49 229 50 233 51 239 52 241 53 251 54 257 55 263 56 269 57 271 58 277 59 281 60 283 61 293 62 307 63 311 64 313 65 317 66 331 67 337 68 347 69 349 70 353 71 359 72 367 73 373 74 379 75 383 76 389 77 397 78 401 79 409 80 419 81 421 82 431 83 433 84 439