sys.argv and while loop

Christopher Myers chris.myers at ingenta.com
Wed May 8 11:23:30 EDT 2002


Your problem stems from the fact that you are assuming that the variable
orbits is an integer, when in fact it is a string.
If you change your while clause to:

    while count < int(orbits)

or, after adding an "import string" to the top of your script (since I'm
not sure if the int function existed in Python 1.3):

    while count < string.atoi(orbits)

It should work fine.

Good luck.  =)

Julia Bell wrote:
> 
> 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

-- 
Christopher Myers, Graduate Software Developer 
Ingenta, Inc.
12 Bassett St.
Providence, RI  02903
ph:  401.331.2014 x 102
em:  chris.myers at ingenta.com
aim: chrismyers001



More information about the Python-list mailing list