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>Denominator cannot be zero. In this case, count is zero.</p>

<p>You may need to use count += 1 instead of count = len(num).</p>

<p>You should check whether the denominator is zero before you do the division.</p>

<p>Suggestion: study python 3 instead of python 2.7. Python 2 will be finally replaced by python 3.</p>

<pre><code>    mySum = 0
    count = 0
    while True:  # this loop keeps attempting to get input until the user enters        an integer
        try:
            num = int(input("input a num "))
        except ValueError:
            print("Input must be an integer.")
            continue
        if num == 0:
            break
        else:
            mySum += num
            count += 1
            print(num)
    avg = mySum / count if count else 0
    print("The average is", avg)  # prints average
</code></pre>

python python-2.7