Help Help Help

Jp Calderone kuran42 at yahoo.com
Thu Nov 8 01:36:07 EST 2001


import sys

def isInteger(x):
   """Checks if argument can be made to represent an int
      Returns 1 if so, 0 otherwise"""

   try:
     int(x)
     return 1
   except:
     return 0

def isPrime(x):
   """Checks if argument is a prime number
      Returns 1 if so, 0 otherwise"""

   if x <= 1:
     return 0
   else:
     for i in range(2, x / 2):
       if x % i == 0:
         return 0
     return 1

def readInt(prompt = 'Enter an integer:'):
   """Read in and return an integer from standard input, printing
      prompt first and checking for valid input"""

   print prompt,
   while 1:
     x = sys.stdin.readline()
     if isInteger(x):
       return int(x)
     else:
       print 'That is not an integer, please try again:',

if __name__ == '__main__':
   while 1:
     x = readInt('Enter a prime number:')
     if isPrime(x):
       for i in range(0, x):
         print i,
       sys.exit(0)
     else:
       print x, ' isn\'t a prime number.'


Kojo Duncan wrote:

> I'm taking a computer science course and I really need help understanding
> python. If you know of any easy to follow tutorials, great emphasis on easy
> to follow, please send me a reply on gyekye_7 at yahoo.com .
> 
> Thanks.
> 
> Here's a lil practice question for you python gurus out there.
> 
> Write a program that lists all prime numbers less than a prime number a user
> inputs. Post on site for all to see.
> 
> 
> 
> 
> 
> 




More information about the Python-list mailing list