March 1st, 2009, 04:38 AM

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 variablesin 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 workingthoughts?
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
March 1st, 2009, 01:02 PM

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 variablesin 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 workingthoughts?
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