Sabih Hasan Sabih Hasan - 1 year ago 66
Python Question

Adding a number to the list if it passes the conditions in another function

so I've written a function which checks if a number is a prime

def prime_checker(prime):
limit = int(math.sqrt(prime))
x = 2
while x <= limit:
if prime % x != 0:
x += 1
if x == limit:
print("%d is prime" % prime)
return True
print("%d Not a prime" % prime)
return False


Now I want to create a function which tests numbers with prime_checker method and append this number to a list if it is prime. My attempt for it is:

def prime_counting():
list_of_primes = []
for x in range(10):
if prime_checker(x) == True:


However, this doesn't work. Is there any way to fix this?

Answer Source

Your prime checker does not work in some cases. Try

def prime_checker(prime):
    limit = int(math.sqrt(prime))
    for x in range(2, limit + 1):
        if prime % x == 0:
            return False
    return True
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download