[Tutor] Unexpected Result in Test Sequence

Emmanuel Ruellan emmanuel.ruellan at laposte.net
Mon Nov 16 20:48:40 CET 2009


In your code, list list1 never gets emptied.

There is another problem : the doc for random.randrange() says it "choose[s]
a random item from range(start, stop[, step])" and range(1, 19) goes from 1
included to 19 _not_ included. Your 'd20' is not a 20-sided dice.

On Mon, Nov 16, 2009 at 8:18 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)
>       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 at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091116/51b032fa/attachment.htm>


More information about the Tutor mailing list