Test where students miserably fail: calculate average of inputted positive numbers, stopping if 99999 is inputted


In [1]:
from operator import add
print("numbers followed by enter - (stop by typing 99999)")
numbers = []
while True:
    i = raw_input("? ")
    try:
        i = int(i)
    except:
        continue
        
    if i == 99999:
        break
        
    if i > 0:
        numbers.append(i)
print("average: %s" % (reduce(add, numbers) / float(len(numbers))))


numbers followed by enter - (stop by typing 99999)
? 99999
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-1-af4015d95e5b> in <module>()
     14     if i > 0:
     15         numbers.append(i)
---> 16 print("average: %s" % (reduce(add, numbers) / float(len(numbers))))

TypeError: reduce() of empty sequence with no initial value

In [ ]: