Intermediate · Functions · 108 of 264
Prime check
Write a function is_prime(n) that returns True when n is a prime number (it has exactly two divisors: 1 and itself). Use it to answer for the number read.
Input: a whole number n (n >= 0). Output: prime or not prime
Examples
Input
7
Output
prime
Input
1
Output
not prime
Input
2
Output
prime
Input
91
Output
not prime
2 more tests check other inputs when you press Check.
Stuck? The idea behind functions
A function, made with def, gives a name to a piece of code, so it can be used again: its parameters are the values it is given, return hands an answer back.
A function that returns lets the caller decide what to do with the answer: print it, compare it, use it in a sum.
def is_even(n):
return n % 2 == 0
print(is_even(4))
print(is_even(7))Watch for: print inside a function is not return: the caller gets None. And the code after return in the same block never runs.