Help Help Help

Jeff Shannon jeff at ccvcorp.com
Thu Nov 8 15:23:51 EST 2001


Jp Calderone wrote:

> 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
> .............
>    while 1:
>      x = sys.stdin.readline()
>      if isInteger(x):
>        return int(x)
>      else:
>        print 'That is not an integer, please try again:',

Why do the int() conversion twice??  By rewriting your while loop as follows, you
eliminate the need for your isInteger() function....

while 1:
    x = sys.stdin.readline()
    try:
        return int(x)
    except ValueError:
        print "That is not an integer, please try again:",

I also wonder why you use sys.stdin.readline() in place of raw_input(), but you
still use print and not sys.stdout.write() .... but I suppose that's just a
trivial bit of inconsistency with only aesthetic significance....  :)

Jeff Shannon
Technician/Programmer
Credit International





More information about the Python-list mailing list