Hi!
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 
1000!

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!
Cheers
José Amoreira


On Monday 16 November 2009 07:18:52 pm kb1...@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)
>         list1.append(t)
>     for value in list1:
>         d+=value
>     print d/1000
>     d = 0
> for value in range(1,100):
>     p()
>
> It works, but I have a logic error somewhere. It runs, and the results
> have a pattern :
> 9
> 19
> 28
> 37
> 47
> 56
> 66
> 75
> 85
> 94
> 104
> 113
> ...
> ...
> 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,
> etc.
> What is causing this?
> _______________________________________________
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor

_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Reply via email to