3.20. First Non-Prime ¶
Write a function named find_first_non_prime to find the first
non-prime number in the given list. If all the numbers are prime, return
False
.
Hint: You can define a helper function to determine whether a number is prime or not then use it in your function.
Sample I/O:
>>> find_first_non_prime([2,3,4,7])
4
>>> find_first_non_prime([2,3,5,7])
False
>>> find_first_non_prime([2,3,51,19])
51
def is_prime(number): # helper function
for i in range(2, number):
if number % i == 0:
return False
return True
def find_first_non_prime(items):
for item in items:
if not is_prime(item):
return item
return False