#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2009
    Posts
    4
    Rep Power
    0

    How do you repeat or loop a python program?


    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?

    Code:
    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
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2009
    Posts
    18
    Rep Power
    0
    Try it this way:

    Code:
    if __name__=='__main__':
        cont_flag = True
        while cont_flag:
           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"
           dummy = raw_input('again (y/n): ').lower()
           if dummy != 'y':
              cont_flag = False
    Originally Posted by Sohvkhan
    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?

    Code:
    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

IMN logo majestic logo threadwatch logo seochat tools logo