By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,818 Members | 1,347 Online
Bytes IT Community
+ Ask a Question
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

P: 15
I'm trying to create a script that will accept a user entered integer and find only one set of prime numbers that add up to that integer but need some help. The script is supposed to only accept even numbers, and if it encounters an odd number it needs to exit with a "please enter only even numbers" message. I've made some progress on the script, but it still isnt quite rite.

I already tried to code an error message for uneven input numbers, and it sort of works, but see the output below. Instead of displaying one "is not even" message, it displayed several... why?

How can I get it to display just one? (I barely know what i'm doing here... but i'm trying hard to learn)

Heres my code as it is now:
Expand|Select|Wrap|Line Numbers
  1.     import math
  2.     def is_prime(p):
  3.         prime = True
  4.         if p%2 == 0:
  5.             print n,"is not even. Please enter only even numbers."
  6.             prime = False
  7.         else:
  8.             for i in range(3, math.ceil(math.sqrt(p/2)), 2):
  9.                 if p % i == 0:
  10.                     prime = False
  11.                     break
  12.                     print n,"is not even. Please enter only even numbers."
  13.                 return prime
  14.     n = int(raw_input('Enter number to test: '))
  15.  
  16.     for p in range(3, n/2, 2):
  17.         if is_prime(p) and is_prime(n-p):
  18.             print n, '= %d + %d' % (p, n-p)
  19.             print "Found two prime numbers that sum to",n
  20.             break
  21.  
Here is the output:

Enter number to test: 62
62 = 25 + 37
Found two prime numbers that sum to 62

Enter number to test: 67
67 is not even. Please enter only even numbers.
67 is not even. Please enter only even numbers.
67 is not even. Please enter only even numbers.
67 is not even. Please enter only even numbers.

Thanks! 8)
Mar 15 '08 #1
Share this Question
Share on Google+
4 Replies


bvdet
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
  1. import math
  2.  
  3. def main():
  4.  
  5.     def is_prime(p):
  6.         prime = True
  7.         if p%2 == 0:
  8.             prime = False
  9.         else:
  10.             for i in range(3, int(math.sqrt(p/2))+1, 2):
  11.                 if p % i == 0:
  12.                     prime = False
  13.                     break
  14.             return prime
  15.     while True:
  16.         n = int(raw_input('Ener number to test: '))
  17.         if n%2:
  18.             print n,"is not even. Please enter only even numbers."
  19.         else:
  20.             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

bvdet
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
  1. import math
  2.  
  3. def is_prime(p):
  4.     prime = True
  5.     if p%2 == 0:
  6.         prime = False
  7.     else:
  8.         for i in range(3, int(math.sqrt(p/2))+1, 2):
  9.             if p % i == 0:
  10.                 prime = False
  11.                 break
  12.         return prime
  13.  
  14. def main():
  15.  
  16.     while True:
  17.         n = int(raw_input('Ener number to test: '))
  18.         if n%2:
  19.             print n,"is not even. Please enter only even numbers."
  20.         else:
  21.             break
  22.  
  23.     for p in range(3, n/2, 2):
  24.         if is_prime(p) and is_prime(n-p):
  25.             print n, '= %d + %d' % (p, n-p)
  26.             print "Found two prime numbers that sum to",n
  27.             return
  28.  
  29.     print "No primes for number %d" % n
  30.  
  31. main()
Mar 15 '08 #4

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

Post your reply

Sign in to post your reply or Sign up for a free account.