0

Again I have tried to move on to another program and I am running into another snag. I am trying to write a program that determines if a number is prime. I know the basic format of what I have is pretty close (we talked about it in class), so I would not like to change it too much if at all possible. I cannot seem to get the program to run, it keeps giving me different error messages when I change things around. As usual any help would be most appreciated. THANKS Here is what I have come up with:

import math

def isprime(n):

print "This program determines if a given number is a prime number\
and will give True if it is and False if it is not."

if n < 2:
return False

elif n == 2:
return True

else:
for x in range(3, int(math.sqrt(n) + 1), 2):
if n % x == 0:
return False
return True

isprime()

2
Contributors
1
Reply
2
Views
9 Years
Discussion Span
Last Post by woooee
0

First, when you call isprime() on the last line, you have to pass a number to test to the function, and you have to receive the result. Then you want to print out the result. As far as error messages, while testing, put some print statements in the body of the program to see what it is doing. Also, use code tags. Your response will be much greater since you aren't being rude.

## added print statements
      stop = int(math.sqrt(n) + 1)
      for x in range(3, stop, 2):
         print "for loop", x, stop, "n%x =", n%x
         if n % x == 0:
            return False
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.