sys.argv and while loop
John Machin
sjmachin at lexicon.net
Wed May 8 16:56:42 EDT 2002
Julia Bell <Julia.Bell at jpl.nasa.gov> wrote in message news:<3CD9376E.BEF43213 at jpl.nasa.gov>...
> Using Python 1.3 on an HP (UNIX) (to write my first Python script):
G'day, Julia. Welcome to Python. *Please* do yourself a favour and
grab a copy of Python 2.2 real soon now. Version 1.3 is not supported
for mission-critical extra-terrestrial use :-)
>
> 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]
You need orbits = int(sys.argv[1])
Python allows comparison between operands of differing types, so you
were comparing an int (count) with a string (orbits). 16 < "16" is
true, as is 17 < "16".
> while count < orbits:
'while' works as advertised, but you will want to become familiar with
the for statement and the range() built-in function:
for count in range(orbits):
do_something()
> print count, orbits
In your original script (w/o the int() fix), replace this with:
print repr(count), repr(orbits), count < orbits, count <
int(orbits)
This will make the int versus string problem plainer.
HTH,
John
More information about the Python-list
mailing list