Here is some code I have been working on for an assignment, basically it finds prime numbers in between two variables-in any case, I have been trouble figuring out a way to restart the program based on user input. Basically the user inputs y to restart and n to quit. Unfortunately, the code I have been coming up with so far is not working-thoughts?

from math import *
def isPrime(n):
    if n%2==0 and n!=2:return False    

    k = n**0.5 ;  m = ceil(k)          
    if k==m:return False
    
    for i in xrange(3,int(m),2):       
        if n%i==0:return False
        
    return True                        

if __name__=='__main__':
    s = input('Enter Start: ')
    e = input('Enter End:   ')
    s|=1                               #if s%2==0:s+=1   
    list = [x for x in range(s,e,2) if isPrime(x)] 
    print "list of prime numbers", list
    print "type y/n to go again or not"
cont_flag = True
while cont_flag:
    isPrime(x)
    dummy = raw_input('again (y/n): ').lower()
    if dummy != 'y':
        cont_flag = False

you have the while loop, which us going to act as your loop. the problem however is that you never query the user for x again. if you read the code, you are going to ask the user for y/n, but never for new numbers.

x is going to be the same through all your looping.

Also you probably want to indent everything under __main__, as well as use something other than "list" as a variable name as it is a reserved word. The way you have it, the code under __main__ is executed, and when finished, the wihile loop is executed, If it makes it easier to understand, create another function to ask the user. Finally, note that the end number is not tested which may or may not be what you want.

if __name__=='__main__':
   dummy = "y"
   while dummy == "y":
       s = input('Enter Start: ')
       e = input('Enter End:   ')
       s|=1                               #if s%2==0:s+=1   
       list_prime = [x for x in range(s,e,2) if isPrime(x)] 
       print "list of prime numbers", list_prime
       print "type y/n to go again or not"
       dummy = raw_input('again (y/n): ').lower()
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.