Scientific Computing with Python Projects - Probability Calculator

Hey everybody,
I have an issue with the last test of the Probability Calculator. I’m off by a small margin and can’t figure out where it goes wrong.
Like you can see in my code in mt.py I printed both lists before and after the draws to find any mistakes. But at least in my draw function everything seems to work fine.
So maybe it’s my experiment function. But I don’t see anything.
Can anybody give me a hint please?

My Code so far

User Agent is: Chrome/111.0.0.0

Challenge: Scientific Computing with Python Projects - Probability Calculator

Link to the challenge:

Not sure if this is it, but seems there might be a problem in your draws method regarding what happens if your draws exceed the number of balls in the hat… gonna leave it at that for now… let me know if you need further clarification.

I don’t get it. I looked at every line of code in debugging mode for an hour. I can’t find anything wrong with it. Furthermore, I made a copy of the draw method where I print every line, and it seems to work like I want it to. Since you said there might be something wrong happening when the hat becomes empty, I tried multiple different ways for copying the list for the refill. It seems like all of them work.
Help :smiling_face_with_tear:

Hi @eliaskirchner! I’m going to go through your code tomorrow and get back with you if no one else has posted something by then or you haven’t figured it out! Good luck until then! :+1:

If you want to catch the error, try it on a hat that doesn’t have enough balls to cover all your draws.

What I believe the problem is:

When you’re hat runs out of balls, instead of returning an array of all the balls that were in the hat, your code instead fills the hat back up, then proceeds with removing balls. Thats why the one test you are failing is the test where there are only 7 balls, but requested 20 draws.

Per the instructions:
The balls should not go back into the hat during the draw, similar to an urn experiment without replacement. If the number of balls to draw exceeds the available quantity, return all the balls.

What your code does:

if len(self.contents_copy) == 0:
				self.contents_copy = list(self.contents)
			pick = random.randrange(0, len(self.contents_copy))
			draws.append(self.contents_copy[pick])

Also, when I originally looked over the code, you were only failing the one test… now it seems you are failing two. Something you must have changed between then and now… maybe something to do with how you delete draws from the hat as the failure said you didn’t reduce the numbers in the hat.

Yeah, that did happen because I switched something for testing. Therefore I didn’t delete the balls from self.content. Now it’s back to one failure.

That’s what I do in the mt.py
I draw more balls then there are available.

import prob_calculator
prob_calculator.random.seed(95)
hat = prob_calculator.Hat(yellow=1,red=1,green=1)
hat.draw_with_print(4)

Here I pull more balls than there are in the hat. I print the lists self.content and draws in every round as well as the list self.content after it gets refilled.
And the result looks fine to me. Please check it out.

“When your hat runs out of balls, instead of returning an array of all the balls that were in the hat, your code instead fills the hat back up, then proceeds with removing balls.”
Maybe it’s because I’m not a native speaker, but I don’t see the difference in these two things.

When @kinome79 says:

When you’re hat runs out of balls, instead of returning an array of all the balls that were in the hat, your code instead fills the hat back up, then proceeds with removing balls. Thats why the one test you are failing is the test where there are only 7 balls, but requested 20 draws.

Per the instructions:
The balls should not go back into the hat during the draw, similar to an urn experiment without replacement. If the number of balls to draw exceeds the available quantity, return all the balls.

He’s telling you that you are not supposed to make any more draws if you draw more balls than are available. You should NOT put the balls back into the hat. Instead, you should RETURN all the balls. Return, in this case doesn’t mean return the balls back into the hat, it means that the function needs to return the values of all the balls that are in the hat.

Let me know if that makes sense!

Wow, didn’t realize it might not be a mistake but rather a misunderstanding due to multiple definitions for the work ‘return’.

Like bsandma1 states, in this case, it doesn’t mean return all your balls into the hat and continue drawing, it means you should stop drawing , and that the return value for your function should be a list of all the balls that were in the hat.

Let us know if that resolves your failures.

OH my god, I can’t believe that was it! I’m so happy. It works and I finally passed the last test. Only took me a week or so :smiley:
Thank you for your patience and help, I appreciate it.

2 Likes