[Tutor] Unexpected Result in Test Sequence

Jeff R. Allen jra at nella.org
Mon Nov 16 20:40:53 CET 2009

When you declare list1 before "def p()" you are making it global. That
means it will keep its values between invocations of p(). When you
start function p, you don't reset list1 to empty. You divide each time
by 1000, and but your list1 list is growing and growing and growing.
That's why the total is growing, not giving the answer you were

Either you need to reset list1 to empty each time p() starts, or (if
you want to accumulate the results between calls to p()) you need to
divide by the true number of items in the list, not by 1000. So
replace /1000 with /len(list1).

However, that brings me to something else interesting. You are doing
an integer divide. That means you'll get integer results, which for
your purposes are much less interesting than floating point ones.
Replace the /1000 with /1000.0 and see what you get. (Move the
declaration of list1 into p() also.)

Another thing... play in the interpreter with range(1,5) and
range(0,5). Can you see another little bug your program has? It is not
doing precisely what you think it is yet...


More information about the Tutor mailing list