Problem with probability calculator

for some reason when i run my code in replit it returns 0.29 everytime, but when i run it in pycharm it returns a slightly different number every time as youd expect,
any ideas how to fix this?.
Thanks in advance .

the error i get is " AssertionError: 0.29 != 0.272 within 0.01 delta"
import copy
import random
# Consider using the modules imported above.
class  Hat:
  def __init__(self, **data):
    self.contents = []
    self.drawn = []
    for k,v in data.items():
      for i in range(v):
        self.contents.append(k)
    self.cont = self.contents.copy()


  def draw(self,n):
    if n > len(self.contents):
      self.drawn.clear()
      self.contents = self.cont.copy()
    for i in range(n):
        self.drawn.append(self.contents.pop(random.randrange(0,len(self.contents) )))
    return self.drawn

def experiment(hat,expected_balls,num_balls_drawn,num_experiments):
  M = -1
  expected =[]
  for k,v in expected_balls.items():
      for n in range(v):
          expected.append(k)
  for n in range(num_experiments):
    count = 0
    hat.draw(num_balls_drawn)
    for i in expected:
      if i in hat.drawn:
        hat.drawn.remove(i)
        count +=1
    if count == len(expected):
      M += 1
  print('count',M)
  sol = "{:.3f}".format((M / num_experiments))
  return float(sol)

One way to check things is to add print() statements of the drawn and expected balls in your final conditional of the experiment to see why things are being misclassified.

I think your draw() method is likely flawed, as the hat object persists over all the experiments and as long as there are enough balls in contents it keeps using the same set of balls. You really need a new hat every experiment, and so it’s better to copy the hat in the experiment than to try to copy the parts in the hat. Plus, the draw appends without always resetting self.drawn, so unused balls will accumulate until the hat is empty.

Could you link the programming challenge?

best,
Dennis

The assertion error is because of the deviation from the probability, the condition is not true, so you get this.
Regarding the Origin of the problem, I think is in your draw methodology, each new experiment from the number of experiments should start with the same balls in hat.
So the mistake is in the counting of M( number of experiments to draw from the expected balls). Hope this helps.

i must have misunderstood the instructions for the draw method, but even now that ive changed im getting another error aboout the random.randrange funtion,

I just fixed that problem but now im getting a weird error about the .random function.
https://replit.com/@NoahWeigert/boilerplate-probability-calculator-2#prob_calculator.py any idea how to fix it ?

The error message shows the testcase: It’s a hat containing 6 balls and a draw of 20.
Your “draw()” sets the hat-content back to the original, if the number of drawn balls is bigger than the content. Which doesn’t solve the problem because the content is still only 6 but you create a randrange for 20 draws → so this runs into an error.

Please check the project description on how you are supposed to handle multiple draws.

Sidenote: The test within the Replit uses a random-seed, which interacts with the random-library to create the exact-same “random” order every time. That’s why running multiple tests will create the exact same result there, while running it in another environment will create different results, unless you use the same seed (looking into the test_module shows the seed as 95)