Trying to rewrite variables
<pre><code>    print("Please enter some integers to average. Enter 0 to indicate you are done.")

#part (a) -- what are the initial values for these variables?
#incomplete
done = 0
mySum = 0
count =  0   
while not done:
    valid = False #don't yet have a valid input
    while not valid: #this loop keeps attempting to get input until the user enters an integer
        try:
            num = int(input())
            valid = True #now the input is valid, and can use it
        except ValueError:
            print("Input must be an integer.")  
    if num == 0:
        break
        mySum = sum(num)
        count = len(num)
        #part (b) -- fill in the inside of this if statement
        #incomplete


    else: print num  #part (c) -- if num is not zero, then... fill in the code
        #incomplete


avg = mySum / count #calculates average
print("The average is", avg) #prints average
</code></pre>

<p>Excuse the comments as this is an assignment from an instructor. As you can see, line 28 of the code shows a divide by zero error for variable mySum. In the while loop I overwrote(or at least tried to) mySum, but still got the division error. Am I going about this correctly or is there some syntax I'm not following? </p>

<p>EDIT: New attempt:</p>

<pre><code>#part (a) -- what are the initial values for these variables?
#incomplete
done = 0
mySum = [] 
count =  len(mySum)  

while not done:
    valid = False #don't yet have a valid input
    while not valid: #this loop keeps attempting to get input until the user enters an integer
        try:
            num = int(input())
            valid = True #now the input is valid, and can use it
        except ValueError:
            print("Input must be an integer.")  
    if num == 0:
        break
        #part (b) -- fill in the inside of this if statement
        #incomplete


    else: mySum.append(num)
    count +=1#part (c) -- if num is not zero, then... fill in the code
        #incomplete


avg = sum(mySum) / count #calculates average

if len(mySum) == 0:
    print "You haven't entered any number"
else: print ("The average is", avg) 
</code></pre>

<p>The "divide by zero" is a consequence of other problems in your code (or incompleteness of it). The proximate cause can be understood from the message: you are dividing by <code>0</code>, which means <code>count</code> is <code>0</code> at that point, which means you haven't actually tracked the numbers you put in. This, in turn, is - because you don't do anything with the numbers you put in.</p>

<ul>
<li><p>In case <code>num == 0</code>, you immediately <code>break</code> the loop; the two statements below do not execute.</p></li>
<li><p>In case of not <code>num == 0</code>, you just print the number; there is not storing of a number into an array that you can <code>sum</code> later, there is no <code>+=</code> summing in an interim variable, and there is certainly no incrementing of <code>count</code>.</p></li>
</ul>

<p>There is two basic ways to do this, hinted to by the above:</p>

<p>List: Make an empty list outside the loop, when 0 is entered just break, otherwise add the new number to the list. After the loop, check the length: if it's zero, complain that no numbers have been entered, otherwise divide the sum by the length. No <code>count</code> required.</p>

<p>Running total: Initialize <code>total</code> and <code>count</code> variables to <code>0</code> outside the loop; again, just break on a <code>0</code>, otherwise add one to the count and the number to the total. After the loop, do the same, just with <code>total</code> and <code>count</code> instead of with <code>sum</code> and <code>len</code>.</p>

python python-2.7