BugLab003 / 020up to 100
mathloopsconditionsmedium

Is It Prime?

The function is_prime(n) should return True if n is a prime number, False otherwise.

There are two bugs hidden in this function. One causes incorrect results for small inputs. The other makes the function silently accept some composite numbers as prime.

Find and fix both bugs.

# your fix
+ must use return
+ must use a loop for / while
hint 2after hint 1
hint 3after hint 2
solution

Read the code. Before running anything: what does the last line below give?

def is_prime(n):
if n < 2:
return True
for i in range(2, int(n**0.5)):
if n % i == 0:
return False
return True
>>> is_prime(9)