443,818 Members | 1,347 Online
Need help? Post your question and get tips & solutions from a community of 443,818 IT Pros & Developers. It's quick & easy.

# Need help making an error message work in a Python script

4 Replies

 Expert Mod 2.5K+ P: 2,851 You placed the print statement in the wrong location. You need a loop to get back to the prompt if the number entered was not even. Convention is to place import statements toward the top of the module outside of your functions. Expand|Select|Wrap|Line Numbers import math   def main():       def is_prime(p):         prime = True         if p%2 == 0:             prime = False         else:             for i in range(3, int(math.sqrt(p/2))+1, 2):                 if p % i == 0:                     prime = False                     break             return prime     while True:         n = int(raw_input('Ener number to test: '))         if n%2:             print n,"is not even. Please enter only even numbers."         else:             break Mar 15 '08 #2

 P: 15 I tried the code you suggested, but it doesnt do the primary function, which is to print out the two prime numbers that sum the input number. Here is the result of the code you provided: Enter number to test: 4 Ener number to test: 4 >>> Enter number to test: 25 Ener number to test: 25 25 is not even. Please enter only even numbers. >>> Ener number to test: 36 >>> Enter number to test: 62 Ener number to test: 62 The problem here is the program is not issuing a "not even" message for 25 untill you enter it a second time. The first time you enter 25, it returns to the original prompt. When I entered 36, it should have at least found that 31 + 5 = 36, but it didnt, it instead returned to the Python prompt. Any ideas why it isnt working correctly? Bottom line, i'm trying to get it to recognize the two first prime numbers that sum up to the input number (if any primes do). If the input number IS even and if it does NOT have two prime numbers as its sum, it should print a "no primes for this number" message or similar. Mar 15 '08 #3

 Expert Mod 2.5K+ P: 2,851 I cannot explain why it does not work for you. Add a return statement after the print statement when two prime number are found. Otherwise the return statement is skipped and you can issue a print statement that no primes were found after exiting the for loop. Expand|Select|Wrap|Line Numbers import math   def is_prime(p):     prime = True     if p%2 == 0:         prime = False     else:         for i in range(3, int(math.sqrt(p/2))+1, 2):             if p % i == 0:                 prime = False                 break         return prime   def main():       while True:         n = int(raw_input('Ener number to test: '))         if n%2:             print n,"is not even. Please enter only even numbers."         else:             break       for p in range(3, n/2, 2):         if is_prime(p) and is_prime(n-p):             print n, '= %d + %d' % (p, n-p)             print "Found two prime numbers that sum to",n             return       print "No primes for number %d" % n   main() Mar 15 '08 #4

 P: 15 got it workin, thanks for the help!!!! Mar 15 '08 #5