440,621 Members | 1,104 Online 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 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 == "y" or moredata == "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() Mar 16 '08 #1
3 Replies

 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 ........# test user input         moredata = raw_input("\nEnter 0 to exit, any other key to continue.")         if moredata == '0':             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 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():     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."     n = validated_input()     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 moredata.lower() == "y":             main()   if __name__ == '__main__':     main() Mar 17 '08 #3

 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 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():     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."       n = validated_input()       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 moredata.lower() == "y":             main()     else:         moredata = str(raw_input("\nWould you like to try another number (yes or no)? "))         if moredata.lower() == "y":             main()   if __name__ == '__main__':     main()   Mar 17 '08 #4 