473,473 Members | 1,959 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

need help implementing a "continue or quit" prompt in this script

15 New Member
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:
Expand|Select|Wrap|Line Numbers
  1. import math
  2. def is_prime(p):
  3.     if p == 2:
  4.         return True
  5.     else:
  6.         return prime_test(p)
  7. def is_even(n):
  8.     return n % 2 == 0
  9. def prime_test(p):
  10.     stop = int(math.ceil(math.sqrt(p))) + 1
  11.     if p % 2 == 0:
  12.         return False
  13.     else:
  14.         for i in range(3, stop, 2):
  15.             if p % i == 0:
  16.                 return False
  17.         return True # is a prime
  18. def validated_input(moredata):
  19.     while moredata[0] == "y" or moredata[0] == "Y":
  20.         input = raw_input("\nEnter an even integer larger than 2: ")
  21.         try:
  22.             n = int(input)
  23.         except ValueError:
  24.             print "*** ERROR: %s is not an integer." % input
  25.         for x in range(2, int(math.ceil(math.sqrt(n))) + 2):
  26.             if n % x == 0:
  27.                 break
  28.             if x == int(math.ceil(math.sqrt(n))) + 1:
  29.                 print "*** ERROR: %s is already prime." % input
  30.         if is_even(n):
  31.             if n > 2:
  32.                 return n
  33.             else:
  34.                 print "*** ERROR: %s is not larger than 2." % n
  35.         else:
  36.             print "*** ERROR: %s is not even." % n
  37.         return None
  38. def main():
  39.     print "This program finds two prime numbers that add up to any even number you enter."
  40.     moredata = "y"
  41.     n = validated_input(moredata)
  42.     if n:
  43.         for p in xrange(2, int(math.ceil(math.sqrt(n))) + 2):
  44.             if is_prime(p) and is_prime(n - p):
  45.                 print "="*40,"\nFound two prime numbers that sum to",n,"!"
  46.                 print n, '= %d + %d' % (p, n - p),"\n","="*40
  47.     moredata = raw_input("\nWould you like to try another number (yes or no)? ")
  48. if __name__ == '__main__':
  49.     main()
Mar 16 '08 #1
3 6270
bvdet
2,851 Recognized Expert Moderator Specialist
Add a loop, possibly while True:. Test on what the user enters. If the string entered is equal to the value required to exit, return.
Expand|Select|Wrap|Line Numbers
  1. ........# test user input
  2.         moredata = raw_input("\nEnter 0 to exit, any other key to continue.")
  3.         if moredata == '0':
  4.             return
Mar 17 '08 #2
sigkill9
15 New Member
I tried that, it didnt work (maybe I didnt put it where it was supposed to go...dunno). But I did get it to work using a different approach, still though i'm not quite getting the results I need.

It does ask the user if they want to continue or not, but after continuing if they enter an odd number or a number less than or equal to 2, the program still quits with no continue message...

Could someone please help me figure out how to get it to ask the continue message after every result?

Heres the code as it is now:

Expand|Select|Wrap|Line Numbers
  1. import math
  2. def is_prime(p):
  3.     if p == 2:
  4.         return True
  5.     else:
  6.         return prime_test(p)
  7. def is_even(n):
  8.     return n % 2 == 0
  9. def prime_test(p):
  10.     stop = int(math.ceil(math.sqrt(p))) + 1
  11.     if p % 2 == 0:
  12.         return False
  13.     else:
  14.         for i in range(3, stop, 2):
  15.             if p % i == 0:
  16.                 return False
  17.         return True # is a prime
  18. def validated_input():
  19.     input = raw_input("\nEnter an even integer larger than 2: ")
  20.     try:
  21.         n = int(input)
  22.     except ValueError:
  23.         print "*** ERROR: %s is not an integer." % input
  24.     for x in range(2, int(math.ceil(math.sqrt(n))) + 2):
  25.         if n % x == 0:
  26.             break
  27.         if x == int(math.ceil(math.sqrt(n))) + 1:
  28.             print "*** ERROR: %s is already prime." % input
  29.     if is_even(n):
  30.         if n > 2:
  31.             return n
  32.         else:
  33.             print "*** ERROR: %s is not larger than 2." % n
  34.     else:
  35.         print "*** ERROR: %s is not even." % n
  36.     return None
  37. def main():
  38.     print "This program finds two prime numbers that add up to any even number you enter."
  39.     n = validated_input()
  40.     if n:
  41.         for p in range(2, int(math.ceil(math.sqrt(n))) + 2):
  42.             if is_prime(p) and is_prime(n - p):
  43.                 print "="*40,"\nFound two prime numbers that sum to",n,"!"
  44.                 print n, '= %d + %d' % (p, n - p),"\n","="*40
  45.         moredata = str(raw_input("\nWould you like to try another number (yes or no)? "))
  46.         if moredata[0].lower() == "y":
  47.             main()
  48.  
  49. if __name__ == '__main__':
  50.     main()
Mar 17 '08 #3
micmast
144 New Member
The program exited because when a value below or equal 2 was entered or a not an integer, the function validate_input returned None.

and if n (None): never happened. The solution on the other hand is quite simple.

Expand|Select|Wrap|Line Numbers
  1. import math
  2.  
  3. def is_prime(p):
  4.     if p == 2:
  5.         return True
  6.     else:
  7.         return prime_test(p)
  8.  
  9. def is_even(n):
  10.     return n % 2 == 0
  11.  
  12. def prime_test(p):
  13.  
  14.     stop = int(math.ceil(math.sqrt(p))) + 1
  15.     if p % 2 == 0:
  16.         return False
  17.     else:
  18.         for i in range(3, stop, 2):
  19.             if p % i == 0:
  20.                 return False
  21.         return True # is a prime
  22.  
  23. def validated_input():
  24.     input = raw_input("\nEnter an even integer larger than 2: ")
  25.     try:
  26.         n = int(input)
  27.     except ValueError:
  28.         print "*** ERROR: %s is not an integer." % input
  29.     for x in range(2, int(math.ceil(math.sqrt(n))) + 2):
  30.         if n % x == 0:
  31.             break
  32.         if x == int(math.ceil(math.sqrt(n))) + 1:
  33.             print "*** ERROR: %s is already prime." % input
  34.     if is_even(n):
  35.         if n > 2:
  36.             return n
  37.         else:
  38.             print "*** ERROR: %s is not larger than 2." % n
  39.     else:
  40.         print "*** ERROR: %s is not even." % n
  41.     return None
  42.  
  43.  
  44. def main():
  45.     print "This program finds two prime numbers that add up to any even number you enter."
  46.  
  47.     n = validated_input()
  48.  
  49.     if n:
  50.         for p in range(2, int(math.ceil(math.sqrt(n))) + 2):
  51.  
  52.             if is_prime(p) and is_prime(n - p):
  53.  
  54.                 print "="*40,"\nFound two prime numbers that sum to",n,"!"
  55.  
  56.                 print n, '= %d + %d' % (p, n - p),"\n","="*40
  57.         moredata = str(raw_input("\nWould you like to try another number (yes or no)? "))
  58.         if moredata[0].lower() == "y":
  59.             main()
  60.     else:
  61.         moredata = str(raw_input("\nWould you like to try another number (yes or no)? "))
  62.         if moredata[0].lower() == "y":
  63.             main()
  64.  
  65. if __name__ == '__main__':
  66.     main()
  67.  
Mar 17 '08 #4

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

Similar topics

5
by: BJ | last post by:
The application I have been developing is great except one flaw. When Windows NT shuts down, it doesn't send the Event to my application (if it did, it would close all of the open forms and kill an...
3
by: Harman Sahni | last post by:
As per this URL http://msdn.microsoft.com/library/en-us/vjref98/html/14_14.asp?frame=true conitnue works on for, while, do... I know it works for foreach as well as I'm using it somewhere. My...
6
by: phong.lee | last post by:
I was wondering why when you create a macro and you choose quit, it close out the whole app? i'm running a macro that does alot of appends and updates and at the end i want to be able to save all...
14
by: Daniel Bass | last post by:
is there an equivalent key word for C++'s "continue" in VB (.net) in this context? CString szLine; szLine = myReader.ReadLine(); while ( !szLine.IsEmpty() ) { if ( szLine(0) == '-' ) {
9
by: Susan Rice | last post by:
I'm running a simple win32 console application and I want to impliment a "Press any key to continue", so I print that prompt, and then what's the easiest way to impliment reading any key? Do I use...
14
by: Jan Schmidt | last post by:
Hi, in a nested do-while-loop structure I would like to "continue" the outer loop. With goto this should be no problem in while-loops. However, for do-while I cannot get it to work (without a...
13
by: xz | last post by:
What if I want the following: vector<intv; // v is loaded by push_back() switch( v.size() ) { case 2: //do something
36
by: mdh | last post by:
May I ask the group this somewhat non-focused question....having now seen "continue" used in some of the solutions I have worked on. ( Ex 7-4 solution by Tondo and Gimpel comes to mind) Is there a...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.