## sigkill9

I've been working on this script all weekend and am almost done with it accept for one last detail; implementing a "process another number?" prompt after a user input is processed.

The script works like this: First, the user enters a number to test (to find the two prime numbers that make up that number). Next the program finds the numbers, outputs them, then quits.

What I want to do is create a prompt after the number has been processed that asks the user if they want to try another number or quit. I tried doing this but it isnt working rite... As it is now, after the user enters yes to continue the script simply exits instead of asking the user for another number.

Here is that output:
This program finds two prime numbers that add up to any even number you enter.

Enter an even integer larger than 2: 6
========================================
Found two prime numbers that sum to 6 !
6 = 3 + 3
========================================

Would you like to try another number (yes or no)? y
>>>

Could someone please show me what i'm doing wrong and how to get it working rite?

Heres my code:

``````import math
def is_prime(p):
if p == 2:
return True
else:
return prime_test(p)
def is_even(n):
return n % 2 == 0
def prime_test(p):
stop = int(math.ceil(math.sqrt(p))) + 1
if p % 2 == 0:
return False
else:
for i in range(3, stop, 2):
if p % i == 0:
return False
return True # is a prime
def validated_input(moredata):
while moredata[0] == "y" or moredata[0] == "Y":
input = raw_input("\nEnter an even integer larger than 2: ")
try:
n = int(input)
except ValueError:
print "*** ERROR: %s is not an integer." % input
for x in range(2, int(math.ceil(math.sqrt(n))) + 2):
if n % x == 0:
break
if x == int(math.ceil(math.sqrt(n))) + 1:
print "*** ERROR: %s is already prime." % input
if is_even(n):
if n > 2:
return n
else:
print "*** ERROR: %s is not larger than 2." % n
else:
print "*** ERROR: %s is not even." % n
return None
def main():
print "This program finds two prime numbers that add up to any even number you enter."
moredata = "y"
n = validated_input(moredata)
if n:
for p in xrange(2, int(math.ceil(math.sqrt(n))) + 2):
if is_prime(p) and is_prime(n - p):
print "="*40,"\nFound two prime numbers that sum to",n,"!"
print n, '= %d + %d' % (p, n - p),"\n","="*40
moredata = raw_input("\nWould you like to try another number (yes or no)? ")
if __name__ == '__main__':
main()``````

## vegaseat 1,720

Move your while loop for more data into function main(). Once you return from validated_input() it will pop out of the loop there.

## sigkill9

Thanks man, that helped, but theres one more small bug I need help with.

When you enter the numbers 4, 10, 16, 22, 26, (probably more) it prints out 2 findings instead of just one. Weirdness... I dont know why it's doing this, does anyone know why?

Heres the output to show you what i'm talking about. 14 worked fine, but...:

Enter an even integer larger than 2: 14
========================================
Found two prime numbers that sum to 14 !
14 = 3 + 11
========================================

Enter an even integer larger than 2: 16
========================================
Found two prime numbers that sum to 16 !
16 = 3 + 13
========================================
========================================
Found two prime numbers that sum to 16 !
16 = 5 + 11
========================================

Enter an even integer larger than 2: 22
========================================
Found two prime numbers that sum to 22 !
22 = 3 + 19
========================================
========================================
Found two prime numbers that sum to 22 !
22 = 5 + 17
========================================

It happens on some numbers, but other even numbers output only one finding... strange huh?

Heres the code as it is now:

``````import math
def is_prime(p):
if p == 2:
return True
else:
return prime_test(p)
def is_even(n):
return n % 2 == 0
def prime_test(p):
stop = int(math.ceil(math.sqrt(p))) + 1
if p % 2 == 0:
return False
else:
for i in range(3, stop, 2):
if p % i == 0:
return False
return True # is a prime
def validated_input(moredata):
input = raw_input("\nEnter an even integer larger than 2: ")
try:
n = int(input)
except ValueError:
print "*** ERROR: %s is not an integer." % input
for x in range(2, int(math.ceil(math.sqrt(n))) + 2):
if n % x == 0:
break
if x == int(math.ceil(math.sqrt(n))) + 1:
print "*** ERROR: %s is already prime." % input
if is_even(n):
if n > 2:
return n
else:
print "*** ERROR: %s is not larger than 2." % n
else:
print "*** ERROR: %s is not even." % n
return None
def main():
print "This program finds two prime numbers that add up to any even number you enter."
moredata = "y"
while moredata[0] == "y" or moredata[0] == "Y":
n = validated_input(moredata)
if n:
for p in range(2, int(math.ceil(math.sqrt(n))) + 2):
if is_prime(p) and is_prime(n - p):
print "="*40,"\nFound two prime numbers that sum to",n,"!"
print n, '= %d + %d' % (p, n - p),"\n","="*40
moredata = str(raw_input("\nWould you like to try another number (yes or no)? "))

if __name__ == '__main__':
main()``````

## jrcagle 77

Two different outputs on 4 are strange, yes. What two outputs are you getting?

Jeff

## sigkill9

The only two possible; 3+1, and 2+2

## a1eio 16

Hi,

i might be missing something, but from what i understand your program, gets an int from the user, validates it, then it enters a for loop and checks each number (p) of the loops range, to see if its prime and if the user's number minus p, is prime. If it is then it prints the results, and carries on till the end of the loop. If you only want one result for each number then break after the first result:

``````if is_prime(p) and is_prime(n - p):
print "="*40,"\nFound two prime numbers that sum to",n,"!"
print n, '= %d + %d' % (p, n - p),"\n","="*40
break``````

However i get the feeling i'm missing some important thing about prime numbers... the solution seems too simple hehehe

## sigkill9

That is the down and dirty "bandaid" fix, it works. But there is another underlying issue that is causing the multiple results.

## jrcagle 77

The reason I said "strange" is that 3+1 is a false positive, since 1 is not prime.

as a1eio pointed out, you will get all possible answers with your algorithm, which is certainly mathematically interesting! (Except for the false positive issue)

Jeff

## sigkill9

Should I get a nobel prize? Or is it not quite that profound... lol.

I'm sure there are many other examples out there that do what mine does, I couldn't be the only one.

I'd like to fix the false positive issue, but its finals week so it will have to wait till the weekend.

In the meantime, could someone offer a solution?

## a1eio 16

there's nothing wrong with your program from what i can see, apart from it printing false positives, everything else is doing as it should, you have a loop that will find all possible values... unless you leave it after the first result.

## sigkill9

It works great after putting the break at the end of the loop. Thanks for all the help guys.

:cool: