Having issues with a program to input multiple numbers from the user, till the user types âDone". To compute their average and print the results
<p>So here is how the program is supposed to work. The user would input something like this and the output would give them the answer.
Input:
1
2
2
1
Done</p>

<p>Output:
1.5</p>

<p>So far I was able to come up with the input question and got it to loop until you put Done.</p>

<pre><code>nums = [] # Empty list.

while True:
    num = input("Enter number, or Done  to end:")
if num == "Done":

    break
else:

    nums.append(int(num))
</code></pre>

<p>I don't know how to make the program perform the calculation to print out the average. I am very new to programming in python and I would appreciate any help I can get on fixing this program. Thanks in advance!</p>

<p>Break while loop when 'Done' is the input, else save the number as float. This throws an error if you try to enter 'finish'. Finally calculate and print the Average.</p>

<pre><code>nums = [] # Empty list.

while True:
    num = input("Enter number, or Done  to end:")
    if num == "Done": # This has to be tested inside the while loop
        break
    # Only append after testing for 'Done'
    nums.append(float(num)) # Ensure valid input by convert it into the desired type.

print('average: %s' % (sum(nums) / len(nums)))
</code></pre>

<p>It is a good practice to give the user useful error messages, when some input is not as the program needs it. It can save the user lots of trouble. If you just want to tell the user what the problem was when an invalid input is given, you can do it with a try statement as below:</p>

<pre><code>nums = [] # Empty list.

while True:
    num = input("Enter number, or Done  to end:")
    if num == "Done":
        break
    try:
        nums.append(float(num))
    except Exception:
        print('Please enter a number. Input "%s" will be ignored.' % num)

print('average: %s' % (sum(nums) / len(nums)))
</code></pre>

python python-3.x