Simple python iteration exercise..stuck with try and except
<p>Write a program which repeatedly reads numbers until the user enters "done". Once "done" is entered, print out the total, count, and average of the numbers. If the user enters anything other than a number, detect their mistake using try and except and print an error message and skip to the next number.</p>

<p>This is what I have.</p>

<pre><code>total = 0
count = 0
average = 0
while True:
    number = input("Enter a number:")
    if number == "done":
        break
    try:
        total += numbers
        count += 1
        average = total / len(number)
    except:
        print ("Invalid input")
    continue
print (total, count, average)
</code></pre>

<p>When I run this, I always get invalid input for some reason. My except part must be wrong.</p>

<p>EDIT:
This is what I have now and it works. I do need, however, try and except, for non numbers.</p>

<pre><code>total = 0
count = 0
average = 0
while True:
    number = input("Enter a number:")
    if number == "done":
        break
    total += float(number)
    count += 1
    average = total / count
print (total, count, average)
</code></pre>

<p>I think I got it?!?!</p>

<pre><code>total = 0
count = 0
average = 0
while True:
    number = input("Enter a number:")
    try:
        if number == "done":
            break
        total += float(number)
        count += 1
        average = total / count
    except:
        print ("Invalid input")
print ("total:", total, "count:", count, "average:", average)
</code></pre>

<p>Should I panic if this took me like an hour?
This isn't my first programming language but it's been a while.</p>

<p>A solution...</p>

<pre><code>total = 0
count = 0
average = 0
while True:
    number = input("Enter a number:")
    if number == "done":
        break
    else:
        try:
            total += int(number)
            count += 1
            average = total / count
        except ValueError as ex:
            print ("Invalid input")
            print('"%s" cannot be converted to an int: %s' % (number, ex))
print (total, count, average)
</code></pre>

<p>Problems with your code:</p>

<ul>
<li>total+=numbers # <em>numbers</em> don't exist; is <em>number</em></li>
<li>len(number) # <em>number</em> is a string. for the average you need count</li>
<li>if is not <strong><em>done</em></strong>, else process it</li>
<li>Use try ... except ValueError to catch problem when convert the <em>number</em> to int.</li>
<li>Also, you can use try ... except ValueError as ex to get an error message more comprehensible.</li>
</ul>

python