Build a Probability Calculator Project - Build a Probability Calculator Project

Tell us what’s happening:

I’m failing “The draw method should behave correctly when the number of balls to extract is bigger than the number of balls in the hat.” But I tried everything and nothing is working, can someone tell me what i’m doing wrong??

Your code so far

import copy
import random


class Hat:

  def __init__(self, **kwargs):
    contents = []
    ball_count = dict(**kwargs)
    for color in list(ball_count.keys()):
      for _ in range(ball_count[color]):
        contents.append(color)

    self.contents = contents

  def draw(self, num_balls):
    if num_balls >= len(self.contents):
      return self.contents

    results = []
    for _ in range(num_balls):
      num = random.randint(0,len(self.contents)-1)
      results.append(self.contents.pop(num))

    return results

def experiment(hat, expected_balls, num_balls_drawn, num_experiments):
  original_hat = copy.copy(hat)
  success = 0
  for _ in range(num_experiments):
    balls = copy.deepcopy(original_hat).draw(num_balls_drawn)
    print(balls)
    for color in list(expected_balls.keys()):
      actual = balls.count(color)
      expected = expected_balls[color]
      if actual < expected:
        break
    else:
      success += 1

  return success / num_experiments

Your browser information:

User Agent is: Mozilla/5.0 (X11; CrOS x86_64 14816.131.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36

Challenge Information:

Build a Probability Calculator Project - Build a Probability Calculator Project

This method should remove balls at random from contents and return those balls as a list of strings. The balls should not go back into the hat during the draw, similar to an urn experiment without replacement.

If you draw all the balls out of the hat, how many balls are left in the hat?

def draw(self, num_balls):
    if num_balls >= len(self.contents):
      return self.contents

If I print self.contents after this, what is the contents?