Here is my fix of your code.
def count_primes_in_list(numbers):
primes = []
for num in numbers:
if num == 2:
primes.append(num)
else:
is_prime = True
for i in range(2, num):
if num % i == 0:
is_prime = False
break
if is_prime:
print (num)
primes.append(num)
return len(primes)
z = [4, 5, 6, 7, 8, 9, 10]
print (count_primes_in_list(z))
I’ll note a few things for beginners to know. Don’t use reserved or type like words for methods and variables, name things descriptively. Change function
to count_primes_in_list
, array
to primes
, x
to numbers
and so on. Your test for a prime is incorrect. A prime is a number greater than 1 that can not be divided by another number greater than 1 and less than its self. In python don’t put () around if tests.
1
solved count prime number in a list