Thread: While loops

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

    Join Date
    Oct 2004
    Posts
    13
    Rep Power
    0

    While loops


    I need help on this code. I have to get 3 numbers from user. start number, end number and count by number. The program is suppose to start with the start number then get up to the end number by adding the count by number to it everytime.
    eg. start=3 end=10,count by=2
    3,5,7,9
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2004
    Posts
    36
    Rep Power
    11

    loop


    i = start

    while i < end:
    print i
    i = i + count_by


    I hope it can help you
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Posts
    624
    Rep Power
    34
    Don't use a while loop for that, use the range() function, that's what it's for!

    Code:
    start = input('Enter start number: ')
    stop = input('Enter stop number: ')
    step = input('Enter step number: ')
    
    for counter in range(start, stop, step):
        print counter
  6. #4
  7. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Better yet, use the xrange() function instead of range(). That way your program won't bale if your numbers are too big .
    programming language development: www.netytan.com Hula


IMN logo majestic logo threadwatch logo seochat tools logo