Why cannot jump out the loop?

Skip Montanaro skip at pobox.com
Thu Apr 29 22:34:55 EDT 2004


    Jinming> I have a very simple python program, which includes a while
    Jinming> loop. But to my surprise, it cannot jump out the while
    Jinming> loop. Does anyone know why?

    Jinming> Here is the program:
    Jinming> ___________________________
    Jinming> #!/usr/bin/env python
    Jinming> import sys
    Jinming> n=sys.argv[1]
    Jinming> i=0
    Jinming> while i<n:
    Jinming>   print "i=",i," n=",n
    Jinming>   i+=1
    Jinming> -----------------------------------------------------

You need to convert sys.argv[1] to an integer.  Try this instead:

    #!/usr/bin/env python
    import sys
    n = int(sys.argv[1])
    i = 0
    while i < n:
        print "i=", i, " n=", n
        i += 1

Comparisons between any two objects generally always succeed (this
facilitates sorting of heterogenous lists), but doesn't always sort in
useful ways:

    >>> "1" > 1000
    True

Skip




More information about the Python-list mailing list