I have to determine if all the numbers in a list are prime numbers and then return a boolean \"True\" or \"False\" statement depending on the outcome. I made some conditional st
You should see the problem the other way around.
If you you find a number which is not prime you should return False, and after your loop end you should return True.
You have a return
and a break
in your if/else
block, you should get rid of them. Also the return
in the else
should be outside, or it will just return whenever he finds a "prime".
def all_primes(xs):
is_prime = None
for i in xs:
if i < 2:
is_prime = False
return is_prime
elif (i % 2 == 0):
is_prime = False
return is_prime
else:
is_prime = True
return is_prime
After this, you should know, that you are not really checking primes. Here is not the most efficient way but its clear how to:
def all_primes(xs):
def checkPrime(n):
if n < 2:
return False
for i in xrange(2, n):
if n%i == 0:
return False
return True
return all(map(checkPrime, xs))
EDIT:
without the map
functions, you just have to iterate with a for
loop:
def all_primes(xs):
def checkPrime(n):
if n < 2:
return False
for i in xrange(2, n):
if n%i == 0:
return False
return True
for n in xs:
if not checkPrime(n):
return False
return True