[Tutor] Unexpected Result in Test Sequence

Alan Gauld alan.gauld at btinternet.com
Tue Nov 17 09:23:37 CET 2009


<kb1pkl at aim.com> wrote in message

> 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 :

> 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.

I'm not exactly sure what you expect but given you are finding the
average of a set of numbers between 1,19 I'd expect the result to be 9.5.
In other words the integer value will vary between 9 and 10, which is what
is happening.

Can you show us a segment of data that manifests the problem
more clearly than the set you have included?

HTH,


-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/ 




More information about the Tutor mailing list