sys.argv and while loop

Julia Bell Julia.Bell at jpl.nasa.gov
Wed May 8 10:34:23 EDT 2002


Using Python 1.3 on an HP (UNIX) (to write my first Python script):

In the following script, the while loop doesn't exit as expected if one
of the variables in the conditional is assigned to equal sys.argv[1].
It works as expected if I explicitly set the variable to the number
(that is entered as the argument).

(stored in executable myscript.py)
#!/usr/local2/bin/python
import sys
count = 0
orbits = sys.argv[1]
while count < orbits:
    print count, orbits
    count = count + 1
    if count > 100:    # use this to prevent the infinite loop it gets
into
        sys.exit ( 1 )

The command line I use is:
myscript.py 16

The while loop does not exit when count becomes > 16 (until it hits the
"if" statement that I included to prevent the infinite loop).
If I replace the "orbits = sys.argv[1]" statement with "orbits = 16" and
run the script without the argument (but still importing sys), the while
loop does exit.

How can I define orbits to equal the value of the command line argument
and later use that variable to simply represent the number?

Julia Bell





More information about the Python-list mailing list