I've built my fair share of computers and figured it was time to learn to take advantage of that hardware by learning to program. So I'm taking some courses online. I'm having some issues writing some programs though, so any help would be appreciated. Right now, I'm stuck on this one...

Write another program that reads 100 numbers from the user and prints out the sum

How do stop it at 100 inputs from the user?

God I want to smack the creators of some of those courses upside the head with a thick book.

Asking any user to enter 100 numbers is simply an atrocity. Let's say the user makes a mistake at the 87th number, you have to build in a way to recover without having to start with the first number again, or the nice piece of hardware may end up on the floor of frustration!

Anyway, which version of Python are you using?

Edited 6 Years Ago by vegaseat: n/a

I would suggest learning file read/write. Then you can put 101 numbers in the file and read the file to test, instead of entering 100 numbers every time you want to test.

Asking any user to enter 100 numbers is simply an atrocity. Let's say the user makes a mistake at the 87th number, you have to build in a way to recover without having to start with the first number again, or the nice piece of hardware may end up on the floor of frustration!

Anyway, which version of Python are you using?

I'm using 2.6.4 IDLE right now. I've messed around with linux OSs like sabayon and ubuntu, but windows 7 is easiest for me to keep on the computer for work purposes.

Have you studied for loops?

We have studied loops, but I'm not sure how to get it to stop at 100 inputs.

I already wrote one to add up numbers until the total reaches 100...like this

# Adding user inputs until counter reaches 100

total = 0
while total < 100:
    total += input("Input a number to add to the total: ")
    print "Your current total is", total

print "You have reached 100."

A few things.

int(raw_input())

is safer then

input()

And second, what if they input a letter? It will just raise an error. Do you want to do something with it?

A few things.

int(raw_input())

is safer then

input()

And second, what if they input a letter? It will just raise an error. Do you want to do something with it?

Ok, I'll edit what i have for the input code. It's a pretty basic program, we've only been in class for 2 weeks and we haven't gone over how to prevent an error from a user input.

Here's what i have so far

# Program to add 100 numbers for a total


print "Now, we will add 100 numbers to get the total."

total = 0

for number in range(1,101):
    total += input("Input a number to add to the total: ")
    print "Only", 100 - number, "more to go..."

print "The total is ",total

It seems to work, unless of course they don't enter a number.

That looks like it works. Let me point out a few places it could be better, simpler:

  • At line (approximately 2) put totalNumber = 100 so that if you ever want to change the count of numbers to ask for, you can do it in one place.
  • For similar reasons, I like to add another line next: prompt = "Please type a number: "
  • Change line 8 to be for number in range(totalNumber): It doesn't matter what is the exact range as long as it has 100 items, so use the "natural" one that starts at 0 and ends just before totalNumber. You will discover that Python prefers 0-based counting, so you might as well get used to it.
  • You can deal with a non-number by either aborting the program or by catching an exception and going on, which is what I'd do. The simplest way is to replace line 9 as
    try:
            total += input(prompt)
        except:
            print("You did something wrong. Skipping this entry.")

    This just ignores the bad input because it is hard to go back and try again using the for loop as written.

This question has already been answered. Start a new discussion instead.