Re: New to Python



On Mar 5, 6:33 pm, "dnwu...@xxxxxxxxx" <dnwu...@xxxxxxxxx> 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

.



Relevant Pages

  • Re: algorithm by eratosthenos
    ... Joe Smith schrieb: ... is a for loop and a test condition. ... while (sum <= N) ... int main ...
    (comp.lang.c)
  • Re: Seg Fault on 2nd Pass
    ... structure stamps is read in as follows: ... If it is of any use, I've posted the FOR loop in its entirety ... double sum; ... int main ...
    (comp.lang.c)
  • Re: Fastest MacIntel Forth?
    ... loop Sum; ... VALUE imb ... VALUE sum ... VALUE sel ...
    (comp.lang.forth)
  • Re: Seriously struggling with C
    ... Do your textbook examples, get used to the various loop constructs, try ... and then outputs their sum. ... int main{ ... or at the top of the array) I really like to do it a bit more cryptic, ...
    (comp.lang.c)
  • Re: While statement
    ... > sum off all the odd numbers. ... > inputed value some of the eben integers and sum of odd integers. ... > start loop ... It sounds like your pseudocode will more or less actually do what the ...
    (comp.lang.java.programmer)

Loading