My code is giving me an error that it’s not passing the probability calculation test. I don’t really know how to debug this, I don’t think it’s a problem with the math since I took that directly from the readme.txt.
In your code the balls are not removed from the hat when drawing: this increases the chance of taking the desired color and thus falsely incraesing the probability.
Can you make the program remove every ball drawn to solve this?
Aside from this, your draw() function is not called anywhere - this could cause balls not being returned and causing a miscalculation.
The function draw() contains an argument n_balls which is not mentioned on other places in your code. What did you mean to use here?
I mean… my logic here was that it should pull a number of balls (e.x 5). See if these balls contain at least the desired balls (e.x 2 reds and 1 yellow), and if it does, add one to the count. It should not remove balls permanently from the hat since if it does you’ll eventually run out of balls, so it puts the balls back after the experiment is over, so it can start the next one.
And my draw function is not being called in the code because I’m not the one who’s suppose to call it. instead, I’m using an altered version of it that doesn’t remove the balls permanently.
The n_balls argument is being used in a “for” loop and an “if” statement! Here:
ret = list()
if n_balls >= len(hat.contents):
return hat.contents
for i in range(n_balls):
x = random.choice(hat.contents)
hat.contents.remove(x)
ret.append(x)
return ret
If you have an idea on how to fix this feel free to alter the code!
Sorry if I cane in a bit strong here . You are very right about the `` draw``` part, my bad! However, when running the code step by step, the draw method is not being used at all.
Regarding the rmoval of balls during drawing them: imagine having a hat in front of you with colored balls: red, red, yellow. If you were asked to take 2 balls out of the hat and calculate the chance of those 2 balls being the 2 redballs in a total of 10 draws, two balls per draw. Chances at the beginning are 1 in 3. So you take one ball: red, and put it aside, since you took it out of the hat. Now there are two balls left in the hat: red and yellow., leaving the chance to draw the other red ball 1 in 2.
If you do not put aside the first red ball, but put it back and then take the 2nd ball in the first draw, the chances on it being the red ball are still 1 in 3.
So by not putting back the drawn ball, you increase the chances of finding the desired ball in a draw.
If you needed to draw the yellow ball it’s even stranger: 1 draw, 2 balls per draw: 1st ball is yellow, you put it back and take the 2nd ball…yellow again! So you were able to take 2 yellow balls out of a hat which contained only 1 yellow ball. That is a bit weird, isn’t it? But that is what is happening in your program at the moment.
Back to the code:
In your program a list is made [red, red, yellow]. With each draw, a ball is selected from the list, but not taken out of the list (-> falsely increasing the chance on the desired ball, or even making it possible to take the same ball twice in a draw) and this is where the calculations go wrong.
Sorry for the long story, I hope I am able to make it a bit clear why the balls should be taken out of the hat with the draw of each ball. So try to find where they are drawn, remove them one by one and put them on another list (of which the last part you did already).
Andrew-1135 gave a really good hint on how to make sure you don’t run out of hats.
Please keep us posted on the progress and do ask again