| | |
need some help implementing a "continue or quit" prompt
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Jan 2008
Posts: 17
Reputation:
Solved Threads: 0
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:
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:
Python Syntax (Toggle Plain Text)
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()
•
•
Join Date: Jan 2008
Posts: 17
Reputation:
Solved Threads: 0
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:
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:
Python Syntax (Toggle Plain Text)
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()
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:
However i get the feeling i'm missing some important thing about prime numbers... the solution seems too simple hehehe
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:
python Syntax (Toggle Plain Text)
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
•
•
Join Date: Jan 2008
Posts: 17
Reputation:
Solved Threads: 0
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?
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?
![]() |
Other Threads in the Python Forum
- Previous Thread: can anyone tel me how to implement this??
- Next Thread: RE: defining and Importing file
| Thread Tools | Search this Thread |
alarm assignment avogadro beginner bluetooth character cmd code customdialog cx-freeze data decimals dictionary directory dynamic error examples exe file float font format function generator gnu graphics gui halp homework http ideas import input itunes java leftmouse line linux list lists logging loop maze module mouse mysqldb number numbers output parsing path port prime programming projects push py2exe pygame pyglet pyqt python queue random recursion schedule screensaverloopinactive script scrolledtext slicenotation sqlite ssh stdout string strings sudokusolver table terminal text thread threading time tkinter tlapse tuple tutorial ubuntu unicode urllib urllib2 variable variables ventrilo verify vigenere web webservice wikipedia windows wxpython xlib






