Re: Why cannot jump out the loop?

Discussion in 'Python' started by Skip Montanaro, Apr 30, 2004.

  1. 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
     
    Skip Montanaro, Apr 30, 2004
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. mike
    Replies:
    2
    Views:
    370
    Gordon Beaton
    Sep 8, 2004
  2. Jinming Xu

    Why cannot jump out the loop?

    Jinming Xu, Apr 30, 2004, in forum: Python
    Replies:
    1
    Views:
    333
    =?iso-8859-1?q?Beno=EEt_Dejean?=
    Apr 30, 2004
  3. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,213
    Smokey Grindel
    Dec 2, 2006
  4. Eadwine Rose
    Replies:
    2
    Views:
    228
    Eadwine Rose
    Oct 15, 2006
  5. Isaac Won
    Replies:
    9
    Views:
    443
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page