Why is my code not passing the last test?

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.

https://replit.com/@Sourour1/boilerplate-probability-calculator-4#prob_calculator.py

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36.

Challenge: Probability Calculator

Link to the challenge:

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?

1 Like

@Brain150 pointed a problem, I wanted to point another:

if all(item in needed_balls for item in temp) is True:

check this line of code, see if there is a problem.

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!

I think it is because i tested it and it should return True if the needed balls are in the temp list, and False otherwise.

Sounds like you need multiple identical Hats. If only there was a module already imported for you which could handle that…

Side notes:

  • You should use self inside Hat instead of hat as recommended by PEP8 (it’s also less confusing to read)
  • It’s better to create new lists with [], not list()

Sorry if I cane in a bit strong here :flushed:. 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 :slight_smile:

congratz, your tests are all passed!

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.