0

I'm trying to make a short program where the user enters as many integers as they want. When they type 'done,' the program will calculate all of the numbers the user entered. So far when I run this program, all I get is "done." Am I close to being finished? Can someone help? Is the While loop good for this procedure?

user_inputs = []
        total = 0

        while 1:
            user_inputs = input("Enter integers. Type done to calculate the sum.")

            if user_inputs == "done":
                print("The sum of your numbers is " + user_inputs)
            else:
                user_inputs = int(user_inputs)

Edited by stealthless

2
Contributors
1
Reply
23
Views
4 Years
Discussion Span
Last Post by slate
0

Errors:
You define user_inputs as a list, then redefine it as a string.
You might have not decided whether you collect all the inputs and then calculate the sum, or you want to calculate it on the fly..
Wrong intendation in the post.
You do not use the total variable.
You do not handle invalid numbers.
You have created an infinite loop.

Should be something like this (untested):

total = 0

while 1:
    user_input = input("Enter integers. Type done to calculate the sum.")

    if user_input == "done":
        print("The sum of your numbers is " + total)
    else:
        try:
            total += int(user_input)
            break
        except ValueError:
            print("invalid number: "+user_input)
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.