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>It's because there is no len(number) when number is an int. len is for finding the length of lists/arrays. you can test this for yourself by commenting out the try/except/continue. I think the code below is more what you are after?</p>

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

<p>note there are still some issues. for example you literally have to type "done" in the input box in order to not get an error, but this fixes your initial problem because you had len(number) instead of count in your average. also note that you had total += numbers. when your variable is number not numbers. be careful with your variable names/usage.</p>

python