(adsbygoogle = window.adsbygoogle || []).push({}); 1. The problem statement, all variables and given/known data

Write a function, is_prime, which takes a single integral argument and returns True when the argument is a prime number and False otherwise. Add doctests to your function as you develop it.

2. The attempt at a solution

Code (Text):def is_prime(n):

x = 1

while x<=n:

x+=1

if n%x==0:

return False

elif x==n:

return True

if __name__ == '__main__':

import doctest

doctest.testmod()

I figure x goes from 2 to n, and if it finds a factor it returns false; otherwise, true. Why doesn't this work?

**Physics Forums | Science Articles, Homework Help, Discussion**

The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

# Homework Help: Write an is_prime function in Python

**Physics Forums | Science Articles, Homework Help, Discussion**