[Tutor] Unexpected Result in Test Sequence
ljmamoreira at gmail.com
Mon Nov 16 20:42:53 CET 2009
Everytime your program calls function p, list1 is appended to. It keeps on
getting bigger and bigger. The function adds all the elements of the list, but
the total is divided by 1000, even if the list is already much longer that
And there's another thing. When var1 and var2 are integer, var1/var2 returns
the integer division, that is, for instance 3/2=1, 9/10=0, etc (try it!).
I bet that if you divide the sum of the values in list1 by len(list1)
(otherwise reset the list, so that it remains only 1000 element long) and
replace the line 'print d/1000' with 'print (d*1.0)/len(list1)' (or 'print
d/1000.0', if you reset the list every time you call p) your problem is over.
Hope this helps!
On Monday 16 November 2009 07:18:52 pm kb1pkl at aim.com wrote:
> Hello Tutor list.
> I'm running a test to find what the experimental average of a d20 is,
> and came across a strange bug in my code.
> import random
> list1 = 
> def p():
> d = 0
> for number in range(1,1000):
> t = random.randrange(1,19)
> for value in list1:
> print d/1000
> d = 0
> for value in range(1,100):
> It works, but I have a logic error somewhere. It runs, and the results
> have a pattern :
> It just adds 10, and every second result, subtracts 1, till it gets to
> 0, and then starts again with 9 in singles, and whatever in the 10's,
> What is causing this?
> Tutor maillist - Tutor at python.org
> To unsubscribe or change subscription options:
More information about the Tutor