Scientific Computing with Python Projects - Probability Calculator

Tell us what’s happening:
In the last test my probability comes out to 0.133 instead of the expected 0.272, and i dont know why.

Your code so far

import copy
import random
from collections import Counter
# Consider using the modules imported above.

class Hat:
  def __init__(self, **colors):
    self.contents = []
    for color in colors:
      #print("cantidad: ", colors.get(color))
      cantidad = colors.get(color)
      while cantidad > 0:
        self.contents.append(color)
        cantidad -= 1
      
  def draw(self, balls_to_draw):
    balls_deleted = []
    #print("self.contents in draw:", self.contents)
    if balls_to_draw > len(self.contents):
      return self.contents
    else:
      while balls_to_draw > 0:
        random_element = self.contents.pop(random.randrange(len(self.contents)))
        balls_deleted.append(random_element)
        balls_to_draw -= 1
    return balls_deleted

def experiment(hat, expected_balls, num_balls_drawn, num_experiments):
  print("start self.contents:", hat.contents)
  count_yes, count_no, m = 0, 0, 0
  counter_num_experiments = num_experiments
  #copy_list = copy.deepcopy(hat.contents)
  hat_copy = copy.deepcopy(hat)
  while counter_num_experiments > 0:
    #print("***************")
    #print("test n°:", counter_num_experiments)
    hat = copy.deepcopy(hat_copy)
    #print("self.contents:", hat.contents)
    #print("len(hat.contents):", len(hat.contents))
    hat_draw = hat.draw(num_balls_drawn)
    #print("hat_draw:", hat_draw)
    draw_agrupped = dict((i, hat_draw.count(i)) for i in hat_draw)
    #print("draw_agrupped:", draw_agrupped)
    #print("expected_balls:", expected_balls)
    for k, v in expected_balls.items():
      if k not in draw_agrupped:
        break
      else:
        if v == draw_agrupped[k]:
          #print("v:", v, "draw_agrupped[k]", draw_agrupped[k])
          count_yes += 1
        else:
          #print("v:", v, "draw_agrupped[k]", draw_agrupped[k])
          count_no += 1
     
    if count_yes > 0 and count_no == 0:
      m += 1

    count_yes, count_no = 0, 0
    print("m:", m)

    counter_num_experiments -= 1

  # print("final m:", m)
  print(m/num_experiments)
  return m/num_experiments
 

Your browser information:

User Agent is: `Brave v1.51.114 Chromium: 113.0.5672.92 (Official Build) (64-bit)’
Challenge: Scientific Computing with Python Projects - Probability Calculator

Link to the challenge:

Link to replit:

0.292 instead of the expected 0.272 if a use:

if v <= draw_agrupped[k]:

I changed the draw method but with the same result:

  def draw(self, balls_to_draw):
    balls_deleted = []
    #print("self.contents in draw:", self.contents)
    if balls_to_draw > len(self.contents):
      return self.contents
    else:
      while balls_to_draw > 0:
        random_element_index = random.choice(range(len(self.contents)))
        random_element = self.contents[random_element_index]
        del self.contents[random_element_index]
        balls_deleted.append(random_element)
        balls_to_draw -= 1
    return balls_deleted```

¡I get it! Something was wrong with the way I was counting match cases.
I think that it was a little bit confusing the expression “An object indicating the exact group of balls to attempt to draw from the hat for the experiment”, because it doesn’t have to be “==” an exact match.