Your for
loop exits right after the first iteration when it checks whether your number is divisible by 2. If your number is even, it will return False
; otherwise, it will return True
.
The solution is not to return True
immediately; wait for the end of all the iterations in the loop instead:
for div in range(2, num):
if num % div == 0:
return False
return True
Alternatively, use the all()
construct:
return all(num % div != 0 for div in range(2, num))