New to Python

Arnaud Delobelle arnodel at googlemail.com
Mon Mar 5 14:14:56 EST 2007


On Mar 5, 6:33 pm, "dnwu... at gmail.com" <dnwu... at gmail.com> wrote:
> 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!!!"

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




More information about the Python-list mailing list