Okay, I'll be honest right up front, I'm in a first year Python class and this is a question I'm supposed to write a program for. Here's the question:

"Write a program that continually reads in numbers from the user and adds them together until the sum reaches 100."

Here's what I have so far:

print "Now, we're going to add up some numbers."
print

number = input("Please enter a number: ")
if number < 100:
    print input("Total is not yet over 100, enter another number: ")
else:
    print ("That's it, you're done!")

I can't figure out how to capture what the user inputs, and then keep adding to it. Please, just give me a clue, or a hint where to look for the answer.

Thank you, I hope to find tolerance here for beginners.

Cheers,
Zorbie

I just started learning python. I haven't had much time to practice so I'm not that good. I have an idea of how this could be done.

number_list = []
in_number = raw_input("Enter a number: ")
number_list.append(in_number)

Let me know how it goes. I'd like to see the solution!

I guess this what you are looking for something like this

#!bin/python

num = int(input("Enter number : ")) #getting the starting number .

while (num < 100): #start the loop .
        print "it's less than 100, add more numbers ."
        another_num = int(input("Enter number : "))
        num = num + another_num #add the value to [num]
     


print "Now the sum of the numbers equals : ", num #Out of the loop

I guess this what you are looking for something like this

#!bin/python

num = int(input("Enter number : ")) #getting the starting number .

while (num < 100): #start the loop .
        print "it's less than 100, add more numbers ."
        another_num = int(input("Enter number : "))
        num = num + another_num #add the value to [num]
     


print "Now the sum of the numbers equals : ", num #Out of the loop

try not to use input(). Use the raw_input() instead. (Its documented in the docs)

Different books will give different advice on the use of input and raw_input. I agree with ghostdog: it's safer to do this:

another_num = int(raw_input("Enter another number: "))

BECAUSE input() allows the user to type in any expression whatsoever, including function calls, and Python will dutifully evaluate it and return the value. That's too much power for the user to wield. Imagine a perverse user:

It's less than 100, add more numbers.
Enter a number: os.remove("C:/Program Files/Microsoft Office/Office/WINWORD.EXE")
It's less than 100, add more numbers.
...

On the flip side, input() is able to handle things like this:

Enter a number: 3.5

whereas int(raw_input()) gives

Enter a number: 3.5
Traceback (most recent call last):
  File "<pyshell#0>", line 1, in -toplevel-
    int('3.5')
ValueError: invalid literal for int(): 3.5

So beginner's books just use input() to avoid the hassles that one gets from converting the string from raw_input.

The 'real' right way to do this is:

num = int(input("Enter number : ")) #getting the starting number .
while (num < 100): #start the loop .
    try: 
        print "the sum is %d, add more numbers ." % num
        another_num = int(raw_input("Enter number : "))
        num = num + another_num #add the value to [num]
   except:
      print "Invalid entry!"
      continue
print "Now the sum of the numbers equals : ", num #Out of the loop

What this does is use raw_input, and then catches user errors as needed.

I hope this helps rather than confuses.

Jeff

This article has been dead for over six months. Start a new discussion instead.