Re: [Tutor] Unexpected Result in Test Sequence

2009-11-17 Thread Alan Gauld
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+

Re: [Tutor] Unexpected Result in Test Sequence

2009-11-16 Thread Emmanuel Ruellan
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,

Re: [Tutor] Unexpected Result in Test Sequence

2009-11-16 Thread Tim Peters
[kb1...@aim.com] > I'm running a test to find what the experimental average of a d20 is, I don't know what "a d20" is, but your code is picking integers uniformly at random between 1 and 18 inclusive. The expected value is therefore (1+18)/2.0 = 9.5. > and came across a strange bug in my code. >

Re: [Tutor] Unexpected Result in Test Sequence

2009-11-16 Thread Jose Amoreira
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 intege

Re: [Tutor] Unexpected Result in Test Sequence

2009-11-16 Thread vince spicer
On Mon, Nov 16, 2009 at 1:18 PM, 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) >

Re: [Tutor] Unexpected Result in Test Sequence

2009-11-16 Thread Jeff R. Allen
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

[Tutor] Unexpected Result in Test Sequence

2009-11-16 Thread kb1pkl
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+=v