#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    28
    Rep Power
    0

    newbie having trouble - defining


    how do I define 'while true' in this program.


    Code:
    count = 0
    while true:
          count += 1
          if count > 10:
              break
          if count == 5:
               continue
          print count
          raw_input('press the enter key to exit')
    # this program is suppose to count from 1 to 10 and skip 5
    Last edited by netytan; April 30th, 2004 at 11:41 PM.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2004
    Location
    Canada
    Posts
    242
    Rep Power
    0
    The while statement will loop as long as the expression next to it is true. Using the word "True" is like using while 1:, or in other words, it will loop forever. You don't need to define the "True" constant, because it is built into Python, so just use "True".

    To answer the question in your other thread: The variable "count" is used to count the amount of interations, and once it passes 10, the loop exits.

    This information is all avaible on Python's website, so please have a look: Python.org
    Last edited by NetBSD; April 30th, 2004 at 11:35 PM.
  4. #3
  5. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    A better way to do this would be to use a while expression instead of True i.e.

    Code:
    count = 1
    while count <= 10:
        if count != 5:
            print count
        count = count + 1
    raw_input('press the enter key to exit')
    you can also use a for loop for this simple task.

    Code:
    for number in range(1, 10): if number != 5: print number
    In this instance using a while True loop is a waste of time but its up to you.

    Hope this helps

    Mark.
    programming language development: www.netytan.com Hula

  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    28
    Rep Power
    0
    Thanks netytan you've been a great help, your code worked.

IMN logo majestic logo threadwatch logo seochat tools logo