New to Python

dnwurtz at gmail.com dnwurtz at gmail.com
Mon Mar 5 14:28:52 EST 2007


On Mar 5, 1:14 pm, "Arnaud Delobelle" <arno... at googlemail.com> wrote:
> 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- Hide quoted text -
>
> - Show quoted text -


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!!!"




More information about the Python-list mailing list