Tell us what’s happening:
Hello! I am having trouble with the Probability Calculator task. I have passed the first 3 tests, but the 4th isn’t passing. I have debugged as many special cases and scenarios that I can think of, but the probability output is correct. Am I missing something? Thanks.
Your code so far
import copy
import random
class Hat:
def __init__(self, **color_ball_amount):
self.contents = []
balls_dict = color_ball_amount
for color in balls_dict:
for i in range(0, balls_dict[color]):
self.contents.append(color)
def draw(self, num_to_draw):
if num_to_draw >= len(self.contents):
temp = self.contents
self.contents = []
return temp
random_list = []
for i in range(0, num_to_draw):
index = int(random.random() * len(self.contents))
random_list.append(self.contents.pop(index))
return random_list
def experiment(hat, expected_balls, num_balls_drawn, num_experiments):
n = num_experiments
m_value = 0
check_list = []
for color in expected_balls:
for i in range(0, expected_balls[color]):
check_list.append(color)
for i in range(0, n):
eHat = copy.deepcopy(hat)
draw = eHat.draw(num_balls_drawn)
status = 0
for item in check_list:
if item in draw:
draw.pop(draw.index(item))
status = 1
else:
status = 0
break
m_value += status
return m_value / n
hat = Hat(black=6, red=4, green=3)
probability = experiment(hat=hat,
expected_balls={"red":2, "green":1},
num_balls_drawn=5,
num_experiments=2000)
print(probability)
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36
Challenge Information:
Scientific Computing with Python Projects - Probability Calculator
https://www.freecodecamp.org/learn/scientific-computing-with-python/scientific-computing-with-python-projects/probability-calculator