New to Python

Discussion in 'Python' started by dnwurtz, Mar 5, 2007.

  1. dnwurtz

    dnwurtz Guest

    I am trying to get a program to add up input from the user to get to
    the number 100 using a loop. However, I am having some issues. Here
    is what I have so far. I know I am just trying to hard, but I am
    stuck. Thank you for any help.

    print "We need to count to 100"

    high_number = 100
    total = 0

    number = input("Enter your first number ")
    sum = number + total
    while sum < high_number:
    print "Not there yet..."
    number = input("Enter another number ")

    print "We are there!!!"
     
    dnwurtz, Mar 5, 2007
    #1
    1. Advertisements

  2. dnwurtz

    Stargaming Guest

    Just reading the error messages would be a good start. You assign `sum`
    to `number + total` but as I see it there's no `number` so far. You have
    to increment some variable with the user's input. (`sum += input(..)`)
    Additionally, `total` and `number` seem pretty redundant. You only need
    two variables, one containing the targeted number, one the current progress.
    By the way, input() is considered pretty evil because the user can enter
    arbitrary expressions. It is recommended to use raw_input() (till Python
    3.0), in your case e. g. int(raw_input()) wrapped in a try-except block.
    HTH,
    Stargaming
     
    Stargaming, Mar 5, 2007
    #2
    1. Advertisements

  3. Please, if you have a problem post the exact error symptoms.
    We can't tell by magic where you are stuck. Please state observed
    and expected behaviour.
    (already mentioned: please don't use input)
    This is a one-time assignment.
    "We" won't reach this point since the while condition never becomes
    False. See above; "sum" is assigned just one time and will not
    change in your while loop. Having the program work as probably
    expected requires a "sum += number" inside the while loop.

    BTW, what's "total" expected to do? It's also assigned only one
    time.

    Regards,


    Björn
     
    Bjoern Schliessmann, Mar 5, 2007
    #3
  4. There is no need for 'sum' and 'total'. Here's a working version of
    your program. Look at the differences with your attempt.

    ----------
    print "We need to count to 100"

    high_number = 100

    total = input("Enter your first number ") # first total is the fist
    number
    while total < high_number:
    print "Not there yet..."
    number = input("Enter another number ")
    total = total + number # Add the new number to the total

    print "We are there!!!"
    ----------

    Good luck !

    As others have pointed out, 'input' is a function to be careful with.
    You could use answer=raw_input(...) and then convert the result to an
    int by using number=int(answer).
     
    Arnaud Delobelle, Mar 5, 2007
    #4
  5. dnwurtz

    dnwurtz Guest


    Thanks for the help guys!!!

    Before I got a chance to read your answer Arnaud, I came up with the
    below. Seems to work also. Yours is easier :).

    print "We need to count to 100"

    high_number = 100
    sum = 0

    sum += input("Enter your first number ")
    print sum
    while sum < high_number:
    print "Not there yet..."
    sum += input("Enter another number ")
    print sum

    print "We are there!!!"
     
    dnwurtz, Mar 5, 2007
    #5
  6. a écrit :
    Where ?

    May I suggest this reading ?
    http://www.catb.org/~esr/faqs/smart-questions.html
    Please re-read *very carefully* the doc for input(). Then switch to
    raw_input() and perform appropriate validation and conversion.
    don't use 'sum' as an identifier, unless you don't mind shadowing the
    builtin sum() function.
    We're not. number won't be automagically added to sum.
     
    Bruno Desthuilliers, Mar 5, 2007
    #6
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.