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

    Join Date
    Nov 2013
    Posts
    5
    Rep Power
    0

    Help With While Loops?


    Hi guys,

    I'm making this program for school that involves while loops, and I can't figure out how to do them. Can anyone please explain them to me? Here is what I have to make the program do:

    The program must continuously ask the user to enter numbers. When the user enters 0, the program will stop asking for numbers and will then give the user the sum and the average of all the numbers they entered.

    How would I go about making a program like this using while loops and if statements? I'm still pretty new to Python so you will have to explain this to me very simply and clearly. Thanks so much for your help!
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    194
    Rep Power
    2
    Here is a small tutorial on while loops.

    Experiment with them. Try to set one up to end on a certain input. I'm afraid you won't get much more help without a little more demonstrable effort.

    -Mek
    Last edited by Mekire; November 15th, 2013 at 02:30 AM.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2009
    Posts
    479
    Rep Power
    33
    When the user enters 0, the program will stop asking for numbers
    The "standard" way to get input with Python.
    Code:
    num = -1
    while num != 0:
        input_num = raw_input("Enter a number ")
        try:
            num = int(input_num)
        except:
            print "\n*****Enter numbers only*****\n"

    Comments on this post

    • Mekire disagrees : Naked except =(
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    5
    Rep Power
    0
    Alright, I figured it out. Thanks, guys!

IMN logo majestic logo threadwatch logo seochat tools logo