By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,621 Members | 1,104 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,621 IT Pros & Developers. It's quick & easy.

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

P: 15
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
Share this Question
Share on Google+
3 Replies


bvdet
Expert Mod 2.5K+
P: 2,851
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

P: 15
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
100+
P: 144
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

Post your reply

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