Using Loops to track user input

Discussion in 'Python' started by hokiegal99, Jul 6, 2003.

  1. hokiegal99

    hokiegal99 Guest

    I don't understand how to use a loop to keep track of user input. Could
    someone show me how to do what the program below does with a loop?

    Thnaks!

    ----------------------------
    #Write a program that reads 10 numbers from the user and prints out the
    sum of those numbers.

    num0 = input("Enter a number: ")
    num1 = input("Enter a number: ")
    num2 = input("Enter a number: ")
    num3 = input("Enter a number: ")
    num4 = input("Enter a number: ")
    num5 = input("Enter a number: ")
    num6 = input("Enter a number: ")
    num7 = input("Enter a number: ")
    num8 = input("Enter a number: ")
    num9 = input("Enter a number: ")

    num = num0+num1+num2+num3+num4+num5+num6+num7+num8+num9

    print num
    ----------------------------------
    hokiegal99, Jul 6, 2003
    #1
    1. Advertising

  2. | Write a program that reads 10 numbers from the user
    | and prints out the sum of those numbers.

    hokiegal ...

    Here is one way ...

    nLoops = 10
    total = 0

    for i in range( nLoops ) :

    num_in = int( raw_input( 'Enter an Integer : ' ) )

    total += num_in

    print total

    --
    Cousin Stanley
    Human Being
    Phoenix, Arizona
    Cousin Stanley, Jul 6, 2003
    #2
    1. Advertising

  3. hokiegal ...

    Another way if you also wish to save the input numbers ...

    nLoops = 10

    list_nums = []

    for i in range( nLoops ) :

    this_num = int( raw_input( 'Enter an Integer : ' ) )

    list_nums.append( this_num )

    total = reduce( int.__add__ , list_nums )

    print
    print list_nums
    print
    print total

    --
    Cousin Stanley
    Human Being
    Phoenix, Arizona
    Cousin Stanley, Jul 6, 2003
    #3
  4. hokiegal99 wrote:
    > I don't understand how to use a loop to keep track of user input. Could
    > someone show me how to do what the program below does with a loop?
    >
    > Thnaks!
    >
    > ----------------------------
    > #Write a program that reads 10 numbers from the user and prints out the
    > sum of those numbers.
    >
    > num0 = input("Enter a number: ")
    > num1 = input("Enter a number: ")
    > num2 = input("Enter a number: ")
    > num3 = input("Enter a number: ")
    > num4 = input("Enter a number: ")
    > num5 = input("Enter a number: ")
    > num6 = input("Enter a number: ")
    > num7 = input("Enter a number: ")
    > num8 = input("Enter a number: ")
    > num9 = input("Enter a number: ")
    >
    > num = num0+num1+num2+num3+num4+num5+num6+num7+num8+num9
    >
    > print num
    > ----------------------------------
    >


    # If you are want to loop a fixed amount of time, as I understand a way
    # would be:
    sum = 0
    for i in xrange(10):
    sum += input("Enter a number: ")
    average = sum/10
    print average

    # I guess however it would be more pythonic, because you do not really
    # need that counter, to do something like:
    entries=0
    sum=0
    while entries < 10:
    try:
    sum += input("%d) Enter a number: " % (entries + 1) )
    entries += 1
    except:
    print "You need to enter 10 values."
    print "The average is %d" % (sum)

    # And when you need you would want to make a "general" tool out of your
    # case...
    entries=0
    sum=0
    try:
    while 1:
    sum += input("%d) Enter a number: " % (entries + 1) )
    entries += 1
    except:
    if entries == 0:
    print "You did not enter any number. Can not make average of
    nothing."
    else:
    print "The average of all %d numbers entered is %d" % (entries,
    sum/entries)
    Behrang Dadsetan, Jul 6, 2003
    #4
  5. Hi hokiegal99 if that's what you want me to call you,

    hokiegal99 wrote:
    > I don't understand how to use a loop to keep track of user input. Could
    > someone show me how to do what the program below does with a loop?


    Your text book or tutorial should show that quite well, but here we go:

    >
    > Thnaks!
    >
    > ----------------------------
    > #Write a program that reads 10 numbers from the user and prints out the
    > sum of those numbers.
    >
    > num0 = input("Enter a number: ")
    > num1 = input("Enter a number: ")
    > num2 = input("Enter a number: ")
    > num3 = input("Enter a number: ")
    > num4 = input("Enter a number: ")
    > num5 = input("Enter a number: ")
    > num6 = input("Enter a number: ")
    > num7 = input("Enter a number: ")
    > num8 = input("Enter a number: ")
    > num9 = input("Enter a number: ")
    >
    > num = num0+num1+num2+num3+num4+num5+num6+num7+num8+num9
    >
    > print num
    > ----------------------------------


    #v+
    sum = 0.0
    for i in range(10):
    sum += int(raw_input("Enter a number: "))
    print sum
    #v-

    If your prof is any good, (s)he reads this newsgroup as well ;-)

    -- Gerhard
    =?ISO-8859-1?Q?Gerhard_H=E4ring?=, Jul 6, 2003
    #5
  6. hokiegal99

    hokiegal99 Guest

    You guys are great! Thanks for all the examples.

    Cousin Stanley wrote:

    > hokiegal ...
    >
    > Another way if you also wish to save the input numbers ...
    >
    > nLoops = 10
    >
    > list_nums = []
    >
    > for i in range( nLoops ) :
    >
    > this_num = int( raw_input( 'Enter an Integer : ' ) )
    >
    > list_nums.append( this_num )
    >
    > total = reduce( int.__add__ , list_nums )
    >
    > print
    > print list_nums
    > print
    > print total
    >
    hokiegal99, Jul 6, 2003
    #6
  7. Behrang Dadsetan wrote:
    > hokiegal99 wrote:
    >
    >> I don't understand how to use a loop to keep track of user input.
    >> Could someone show me how to do what the program below does with a loop?
    >>

    >
    > # If you are want to loop a fixed amount of time, as I understand a way
    > # would be:
    > sum = 0
    > for i in xrange(10):
    > sum += input("Enter a number: ")
    > average = sum/10
    > print average
    >


    Ok, I just looked up the Reference documentation and
    int(raw_input("prompt text")) would be probably better than my suggested
    input() - like in the other anwsers you received.


    One should maybe note that if the end average calculated should be a
    float, then you need to use

    sum = 0.0

    and should the user entering the data be allowed to use float values you
    will probably want to use

    float(raw_input("prompt text"))



    Regards, Ben.
    Behrang Dadsetan, Jul 6, 2003
    #7
  8. hokiegal99

    hokiegal99 Guest

    Perl's motto is "there's more than one way to do it," right? Well, after
    reading all of these responses, I think Python could make the same
    claim. Thanks again guys!


    Bob Gailer wrote:

    > Not to neglect the 1-liner:
    >
    > import operator; reduce(operator.add,[int(raw_input("Number>")) for i in
    > range(10)])
    >
    > Bob Gailer
    >
    > 303 442 2625
    hokiegal99, Jul 7, 2003
    #8
  9. hokiegal99

    Peter Hansen Guest

    Gerhard Häring wrote:
    >
    > hokiegal99 wrote:
    > > I don't understand how to use a loop to keep track of user input. Could
    > > someone show me how to do what the program below does with a loop?

    >
    > Your text book or tutorial should show that quite well, but here we go:

    [snip]
    > If your prof is any good, (s)he reads this newsgroup as well ;-)


    You missed this one Gerhard. In another thread, hokiegal99 already
    told us "I am a funeral director trying to write a small program that
    calculates the number of years, months and days a person has lived by
    entering the year, month and day of their birth. ..."

    Not homework this time. :)

    -Peter
    Peter Hansen, Jul 7, 2003
    #9
  10. On Sun, 06 Jul 2003 22:50:55 -0400, rumours say that Peter Hansen
    <> might have written:

    >Gerhard Häring wrote:
    >>
    >> hokiegal99 wrote:
    >> > I don't understand how to use a loop to keep track of user input. Could
    >> > someone show me how to do what the program below does with a loop?

    >>
    >> Your text book or tutorial should show that quite well, but here we go:

    >[snip]
    >> If your prof is any good, (s)he reads this newsgroup as well ;-)

    >
    >You missed this one Gerhard. In another thread, hokiegal99 already
    >told us "I am a funeral director trying to write a small program that
    >calculates the number of years, months and days a person has lived by
    >entering the year, month and day of their birth. ..."


    This is true. However, doesn't hokiegal99's comment at the start of the
    example code:

    > #Write a program that reads 10 numbers from the user and prints out the
    > sum of those numbers.


    seem a little 'textbookish'? Who's the first person of this imperative
    sentence, and who's the user? :) Although it's not improbable that
    hokiegal99 is indeed a funeral director having their first contact with
    programming using a textbook. I remember that it took me a couple of
    days to understand loops (at the age of eleven)...

    BTW computers really spoil us... now that I visited the memory lane, I
    recall that, having a chemistry exam at the age of seventeen, it took me
    a quarter of an hour to solve a generic exercise and ten more minutes to
    remember how to do a division by pen and paper (ok, I should take
    anxiety into account, but it's still funny :)
    --
    TZOTZIOY, I speak England very best,
    Microsoft Security Alert: the Matrix began as open source.
    Christos TZOTZIOY Georgiou, Jul 21, 2003
    #10
    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. John
    Replies:
    0
    Views:
    351
  2. =?iso-8859-1?Q?Bruno_Mendon=E7a?=

    Track Domain User Logons and Logoffs

    =?iso-8859-1?Q?Bruno_Mendon=E7a?=, Jun 3, 2004, in forum: ASP .Net
    Replies:
    5
    Views:
    4,900
    John Saunders
    Jun 4, 2004
  3. tshad

    Track a session user

    tshad, Mar 4, 2005, in forum: ASP .Net
    Replies:
    5
    Views:
    439
    tshad
    Mar 14, 2005
  4. Mel

    How to Track User Login

    Mel, Aug 15, 2007, in forum: ASP .Net
    Replies:
    11
    Views:
    1,254
    Dan Colgan
    Sep 4, 2007
  5. Me
    Replies:
    2
    Views:
    233
Loading...

Share This Page